vmonteco
vmonteco

Reputation: 15393

Django url name, some points I'm not sure about

I'm following the steps of the django tutorial (part 4). And I'm here. There is this tag :

{% url 'polls:vote' question.id %}

which triggers the following line in the urls.py file :

url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

I want to be sure about these points :

  1. question.id is the value passed to the template engine to render the template.

  2. The variable part ((?P<question_id>[0-9]+)) in the regex will be replaced by the first argument in the url tag (question.id).

  3. The name of the variable part (question_id) is just the name the view will use to handle this value (as an argument). So this variable part may have no name (like just r'^([0-9]+)/vote/$').

  4. There could be several variable parts (and then several arguments passed to {% url %} tag).

Could anyone confirm this?

Thank you!

Upvotes: 0

Views: 135

Answers (1)

Patrick Beeson
Patrick Beeson

Reputation: 1695

A few things:

  1. question.id in the url tag is the value passed to the url pattern via the view. The template is specified in the view, but this value ensures a unique object (question) in the context.
  2. Correct
  3. The name of the view is "vote" not question_id. This is shown in the url pattern (views.vote)
  4. You can pass as many values as needed via the url tag in order to match up with your pattern. Just ensure these values are made available via your view.

Upvotes: 1

Related Questions