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:

  1. attempt load database slug matches test1
  2. if exists, fine. display.
  3. 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:

  1. 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 
  2. 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 
  3. 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

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

c# - Convert XDocument to byte array (and byte array to XDocument) -