I'm building a system using Flask which contains both a website and an api for an app. I've got a before_request defined for the webviews as follows:
@app.before_request
def before_request():
g.user = current_user
# And I do some more stuff here..
I've got my views in a folder based structure like this:
views (folder)
---------------
- __init__.py
- apiviews.py
- webviews.py
Because I'm using a token based login system for the api I now want to define a different before_request for all the apiviews. Is there a way that I can do this? Maybe I need to define a decorator or something? All tips are welcome!
You cannot use a before_request
hook for specific views, not in the same app.
Your options are to:
Blueprint
for your API and website; you can register a before_request
per blueprint and it'll be applied to the views for that blueprint only.before_request
.before_request
handler; if request.path.startswith(...)
style testing.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With