jack
jack

Reputation: 147

Flask -weird routing and URL error?

I have 2 (similar) routes in my application. One is for a page with a single image on it, and one is for a page with multiple images on it. The variable used to make a unique url is just randomly generated, the routing_id(single image page) is 7 chars and gallery_routing is 6. The routes look like this:

@app.route('/<routing_id>')
def display_image_page(routing_id):
    stuff
    return render_template("image-page.html", template variables go here)

@app.route('/<gallery_routing>' )
def display_gallery(gallery_routing):
    other stuff
    return render_template("gallery-page-vertical.html", other template vars)

In the functions that call them they are generated like any other urls:

url_for('display_image_page', routing_id=routing_id)

url_for('display_gallery', gallery_routing=gallery_routing)

While testing my app, if I try to access these URLs I get weird errors, like getting a database error from a function that shouldn't have even been run when that url was visited, or just plain 404s. All that these urls are mapped to is just a couple of functions that do some stuff and return a render_template. If I change the URL rules up a little by adding a trailing slash to the first one, and another couple of slashes and some text before the second one(no trailing slash), then they work perfectly:

@app.route('/<routing_id>/')

@app.route('/arbitrary_text/<gallery_routing>')

What is even going on here? I'm not accessing files directly, just pages that display files on them, so why are slashes changing things? I don't want trailing slashes on either of these, nor do I want to have another subdirectory before my gallery_routing. I have about 3 other routes in my app, which are just to static pages that look like:

@app.route('/info')
@app.route('/thanks')

They all work fine. The only change I'm seeing with the ones that aren't working is a variable in the url rule. If it makes a difference I'm using a subdomain for a different route(the one that is the link to the actual image files), and am using a custom server name, so I'm accessing the app at http://local.dev:5000/, but I don't see how this would change things? I've read the docs on URL building and couldn't find anything of help. Thank you.

Upvotes: 0

Views: 490

Answers (1)

akaRem
akaRem

Reputation: 7618

When you say @app.route('/<routing_id>') or @app.route('/<gallery_routing>' ), you are trying to manage the same url pattern in different views.

Look:

/abracadabra -> /<routing_id> 
    ---> routing_id = abracadabra
/abracadabra -> /<gallery_routing> 
    ---> gallery_routing = abracadabra

App can't guess what you mean

And of course, this works

@app.route('/<routing_id>/')
@app.route('/arbitrary_text/<gallery_routing>')

because

/arbitrary_text/abracadabra -> /<routing_id> 
    -x-> routing_id = abracadabra
/arbitrary_text/abracadabra -> /arbitrary_text/<gallery_routing> 
    ---> gallery_routing = abracadabra

Nothing wierd

Upvotes: 1

Related Questions