librelist archives

« back to archive

route('/') isn't adding a trailing slash

route('/') isn't adding a trailing slash

From:
Forest
Date:
2011-02-03 @ 20:09
Hi, all.

I like the way Flask automatically redirects browsers to add a trailing slash,
but it isn't working on my application's root (index) view.  This isn't
noticeable when my application is mounted at the root of a domain, but mine is
mounted in a subdirectory using mod-wsgi:

WSGIScriptAlias /myapp /path/to/myapp/modwsgi/stub.py

The result is that when someone visits http://mysite/myapp, none of the
application's relative urls work.  (This is expected if the trailing slash is
missing, because a relative url like static/image.png means something
different depending on whether the current page is http://foo/bar or
http://foo/bar/)

The Flask docs say it adds a trailing slash when a rule ends in a slash, so I
guess Flask must interpret the slash in @app.route('/') as being at the
beginning instead of at the end.  How can I get Flask to add the trailing
slash in this case?

Cheers,

Forest

Re: [flask] route('/') isn't adding a trailing slash

From:
Marco D. Adelfio
Date:
2011-02-03 @ 20:35
This is very hack-ish, but I had the exact same problem with mod_wsgi
in a subdirectory and came up with this.  I added these lines at the
start of my root view to do the redirection:

    if ('REQUEST_URI' in request.environ and
        not request.environ['REQUEST_URI'].endswith('/')):
        return redirect(url_for('my_root_view').rstrip('/') + '/')

Hoping somebody else has a more elegant solution.

Marco


On Thu, Feb 3, 2011 at 3:09 PM, Forest <list8a.forest@tibit.com> wrote:
> Hi, all.
>
> I like the way Flask automatically redirects browsers to add a trailing slash,
> but it isn't working on my application's root (index) view.  This isn't
> noticeable when my application is mounted at the root of a domain, but mine is
> mounted in a subdirectory using mod-wsgi:
>
> WSGIScriptAlias /myapp /path/to/myapp/modwsgi/stub.py
>
> The result is that when someone visits http://mysite/myapp, none of the
> application's relative urls work.  (This is expected if the trailing slash is
> missing, because a relative url like static/image.png means something
> different depending on whether the current page is http://foo/bar or
> http://foo/bar/)
>
> The Flask docs say it adds a trailing slash when a rule ends in a slash, so I
> guess Flask must interpret the slash in @app.route('/') as being at the
> beginning instead of at the end.  How can I get Flask to add the trailing
> slash in this case?
>
> Cheers,
>
> Forest
>
>