Z
Z
zlodiak2019-12-03 11:46:20
Python
zlodiak, 2019-12-03 11:46:20

Is there a common controller?

I have several routes that display multiple pages:

from flask import Flask, render_template, request, Response, make_response, redirect

app = Flask(__name__)

@app.route('/')
def auth():
    return render_template('auth.html')

@app.route('/page0')
def admin():
    return render_template('page0.html')

@app.route('/page1')
  return render_template('page1.html')

There is a large piece of code that must be executed on every route. Please advise how to do it.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2019-12-03
@zlodiak

If this "big piece" just needs to be executed (without any parameters) then I would stuff it into before_request

S
szelga, 2019-12-03
@szelga

look towards Middleware .

V
Vladimir Kuts, 2019-12-03
@fox_12

Read about functions in python

D
DzenRoamer, 2019-12-04
@DzenRoamer

You can also make a decorator, then you won’t need to wedge into the function code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question