3

From the Flask docs:

def index():
    pass
app.add_url_rule('/', 'index', index)

It also says:

endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint

Indeed, if I do app.add_url_rule('/', None, index) it all seems to work fine. (I'm not sure of the terminology and what an "endpoint" actually is.)

Some further questions:

  1. Is there any reason to specify the second argument?

  2. Is the second argument indeed the "endpoint"?

  3. What are the benefits/drawbacks of (not) specifying the second argument?

  4. What if the same name is used in another url rule? Is the first overwritten? Is this intended?

2 Answers 2

2

In this case, an endpoint is simply a term for a valid URL of your application. From Wikipedia:

In service-oriented architecture, an endpoint is the entry point to a service, a process, or a queue or topic destination

As for naming your URLs - yes, it is optional. There are some benefits to defining names. When using *url_for*() or redirect() for example, you can specify the url's name as a shortcut. This comes with the added benefit of allowing your to change your url structure without changing every line of code that interacts with that url, since it only references it by name.

So in your example, you could reference your index url like this:

return redirect('index')

For your last question, I'm not sure what would happen. It likely would error when trying to resolve the url with that name. Either way, there's no reason to define two different urls with the same name.

Sign up to request clarification or add additional context in comments.

Comments

1

The reason for the second argument is the following:

Let's say you're designing a website and you have /login be the page where your users enter their username, password, OpenID, whatever. Upon successfully logging in, you might want to send them to /. In Flask, the canonical way to do that is:

return redirect(url_for('index'))

Where 'index' is the name of the function you defined to be the handler for /, e.g.,

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

If you instead do:

def index():
    return render_template('index.html')

app.add_url_rule('/', None, index)

It will work just fine when your user requests / explicitly, but when your users successfully login they'll be faced with a horrible error message and have to visit / manually.

In short, it's the proper thing to do.

Also, you should note that

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

And

def index():
    return render_template('index.html')

app.add_url_rule('/', 'index', index)

Are exactly the same and in the both of these last two snippets redirect(url_for('index')) will work perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.