13

I'm using Flask 0.8.

How to have an alias of a URL like this:

@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 my hope to use an alias because of DRY of processing /index

Someone knew the solution?

thanks pepperists.

hof0w
  • 205
  • 1
  • 3
  • 5

3 Answers3

24

This should work. But why do you want two URL's to display the same thing?

@app.route('/')
@app.route('/index')
def index():
    ...
FogleBird
  • 74,300
  • 25
  • 125
  • 131
9

As is written in URL registry doc of Flask :

You can also define multiple rules for the same function. They have to be unique however.

@app.route('/users/', defaults={'page': 1})
@app.route('/users/page/<int:page>')
def show_users(page):
    pass
satoru
  • 31,822
  • 31
  • 91
  • 141
2

I don't know if Flask has a way to assign more than one URL to a view function, but you could certainly chain them like this:

@app.route('/')
def root():
    return index()

@app.route('/index')
def index():
    # Real processing to display /index view
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662