python - Skip/pass over view function so the next can execute in Flask -
i have flask app has following:
@app.route('/') def home(): return "homepage" @app.route('/<slug>') def feature(slug): return "feature: " + slug @app.route('/<path:url>') def catch(url): return "catch: " + url
this works in in following true:
get /
=> "homepage"get /test1
=> "feature: test1"get /test/2
=> "catch: test/2"
eventually, database driven. features, retrieved , displayed based on slug. catch
, they'll loaded database , may result in behaviour such redirect, or return 404. none of problem.
my question this: in /test1
example, i'd achieve following behaviour:
- attempt load database slug matches
test1
- if exists, fine. display.
- if not exist, i'd "fall through"
catch
view function.
point 3 part don't know how achieve. seems ought exist, can't find kind of "fall through next matching view function" behaviour anywhere. possible , if so, missing?
you want use conditional logic routes - it's not best point routes.
however:
you can call route on condition, in cases
request
fields can different call , direct route call:@app.route('/<slug>') def feature(slug): if slug_in_database(slug): return "feature: " + slug return catch(slug) @app.route('/<path:url>') def catch(url): return "catch: " + url
solution right
request
object:@app.route('/<slug>') def feature(slug): if slug_in_database(slug): return "feature: " + slug app.test_request_context(url_for('catch', url=slug)) return catch(slug) @app.route('/<path:url>') def catch(url): return "catch: " + url
but condition:
@app.route('/<path:url>') def feature_or_catch(url): slug = url if '/' not in slug , slug_in_database(slug): return "feature: " + slug return "catch: " + url
Comments
Post a Comment