How to get URL alias in Python Flask?

I am using Flask 0.8.

How to get a URL alias, for example:

@app.route('/') def index(): # I want to display as http://localhost/index, BUT, I DON'T WANT TO REDIRECT. # KEEP URL with only '/' @app.route('/index') def index(): # Real processing to display /index view 

So why I hope to use an alias due to DRY processing / index

Did someone know the solution?

thanks peppers.

+8
python flask jinja2 werkzeug
source share
3 answers

That should work. But why do you want two URLs to display the same thing?

 @app.route('/') @app.route('/index') def index(): ... 
+14
source share

As written in the Flask registry registry URL :

You can also define multiple rules for the same function. They must be unique, however.

 @app.route('/users/', defaults={'page': 1}) @app.route('/users/page/<int:page>') def show_users(page): pass 
+5
source share

I don't know if Flask can assign more than one URL to a view function, but you can probably link them like this:

 @app.route('/') def root(): return index() @app.route('/index') def index(): # Real processing to display /index view 
+2
source share

All Articles