P
P
pcdesign2016-10-24 10:19:34
Flask
pcdesign, 2016-10-24 10:19:34

Which is better to use: decorator or before_request?

There are options:
1) Use a decorator for private parts of the site where authorization is required:

@login_required
@app.route('/members/' )

And if there are 100500 routes in the closed part of the site, then you need to add 100500 lines with @login_required.
Yes, and forget you can inadvertently stick it.
2) Use before_request once.
@app.before_request
def before_request():
    user_id = session.get('user_id', None)
    if not user_id and request.path.startswith('/members/'):
        return redirect(url_for('login'))

Which option is correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Biryukov-Romanov, 2016-10-24
@pcdesign

There is no correct option.
See what you need specifically in your case.
You can compare them for response speed, memory consumption and more, more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question