Flask app route for paths that start with X
I would do a
catch-all url and then, try to use a
wildcard with it from inside the view:
@app.route('/<path:text>', methods=['GET', 'POST']) def all_routes(text): if text.startswith('favicon'): #do stuff else: return redirect(url_for('404_error'))
you can use
string wouldn't catch
/ strings. so if string is used,
url's containing something like
favicon/buzz wouldn't be cached by it,
path in the other hand would catch
/'s too. so you should go with first option.
you can look at routing documentation in flask site. and you
should create a better conditional than
if x in Y because it will fail if you were passed something like
I work as a IT consultant for a small but capable consulting firm in Bergen, Norway. I also enjoy technology in my spare time. My latest project is http://octomy.orgUpdated on June 28, 2022
Lennart Rolland 11 months
I am a beginner with Flask and python. I want to create a handler function for paths that start with "/favicon". For example the following should be handled:
The following should not be handled:
If Flask supporeted wildcards, it would be "/favicon*"
EDIT: I don't need support for regular expressions.
How can I do this?
ZdaR over 5 yearsYou want all routes:
favicon_baretc. to point same handle, then you may look at this thread
Lennart Rolland over 5 yearsYes exactly. If it was a wildcard it would be /favicon*
Lennart Rolland over 5 yearsI would not say it's a duplicate because it is much more spesific than the other question. Besides, I don't care if it is solved using regex or not.
ZdaR over 5 yearsIt can be perfectly solved with regex, I guess it would be the best way to reduce redundancy in this case(flask). However Django framework has more convenient support for regexs'.
senaps over 5 yearsplease let me know if the answer did what you wanted if you found another way to do it.