D
D
Dimas42018-08-29 09:23:53
Python
Dimas4, 2018-08-29 09:23:53

How to make a decorator for all routes in Flask?

I have many routes, and they all have one decorator attached to them. How can I make it so that it is not necessary to prescribe a decorator for each route, but it is somehow automatically added?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2018-08-29
@pcdesign

You can shove this economy into @app.before_request

R
Roman Kitaev, 2018-08-29
@deliro

This can be done, for example, by introspection. But this is very implicit, in python it is not accepted. The essence is this: you go through the module, look for all the functions, create routes based on their names and knit functions to them.
You can make a router like in django. IMHO, this is a much better solution, which allows you not to build an architecture turned inside out, like a flask with circular dependencies and routes that you can find.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question