librelist archives

« back to archive

Trailing slash problem with DispatcherMiddleware

Trailing slash problem with DispatcherMiddleware

From:
Olivier Scherler
Date:
2011-04-26 @ 18:43
Hello everybody,

I am new to Flask and I am struggling with a trailing slash problem. I 
create a main application and a sub application like this:

(Code below or Gist at <https://gist.github.com/942816>)

* * * * *

# mount.py
from werkzeug.wsgi import DispatcherMiddleware

from flask import Flask
from sub_app import app as subapp

app = Flask( __name__ )

# dispatch by path
app.wsgi_app = DispatcherMiddleware( app.wsgi_app, {
    '/sub': subapp.wsgi_app
} )

@app.route('/')
def hello_world():
	return 'Hello, World!'

if __name__ == '__main__':
	app.run( debug = True )

* * * * *

# sub_app/__init__.py
from flask import Flask

app = Flask( __name__ )

@app.route('/')
def hello_moon():
	return '''<html><body>Hello, Moon! <img src="static/moon.gif"></body></html>'''

if __name__ == '__main__':
	app.run()

* * * * *

Unfortunately, if I visit http://domain.com/sub, a trailing slash is not 
appended by Flask/Werkzeug, and all the relative URLs on my home page 
fail. In this example, the static/moon.gif.

Any ideas? I will be glad to provide further information a needed, but 
right now I don’t know what to add.

Thanks in advance.

Olivier