Vitalii Ponomar
Vitalii Ponomar

Reputation: 10936

Django variable: declare in urls

In urls.py:

(r'^bbb/id(?P<user_id>[0-9]+)/$', 'django.views.generic.simple.direct_to_template,
    {'template': 'some.html', 'extra_context': {'user_id': user_id}}),

In some.html: {{ user_id }}

But there is an error: name 'user_id' is not defined (in urls.py)

So, how to declare that variable in urls.py and send it directly to 'some.html'???

Thanks.

Upvotes: 0

Views: 206

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599600

You don't need to put it in extra_context. It's already captured in the URL, so is present in the params dictionary in the template: {{ params.user_id }}.

See the documentation - and also note that these old function-based generic views are deprecated, and you should be using the class-based TemplateView.

Upvotes: 6

culebr&#243;n
culebr&#243;n

Reputation: 36473

The variable really isn't declared in that python code. :) You do not need to set this variable in the view context. The view will receive named matches as **kwargs.

Upvotes: 0

Related Questions