I have following routs:
url(r'^future/programs/$', main.programs, {'period': 'future'}),
url(r'^past/programs/$', main.programs, {'period': 'past'}),
When I try to display link in template, using template tag url like this
{% url main.views.main.programs %}
I always get link /past/programs/. When I try like this
{% url main.views.main.programs period="future" %}
I get an error:
Caught NoReverseMatch while rendering: Reverse for
‘main.views.main.programs’ with arguments ‘()’ and keyword arguments
‘{‘period’: u’future’}’ not found.
How i can display link to /future/programs/?
I think you might want to approach it with one single url pattern:
and in your view:
and in templates:
In your approach, you are mistaking the forward flow with the reverse flow, i.e. the extra keyword arguments of the url conf with the keyword arguments that are passed to match a pattern.
The former is extra data you are allowed to pass to a view when it is matched (i.e. when a user goes to /future/programs/, the pattern is matched and
period=futureis passed to the view), the latter is the actual data used to match the url (i.e. theperiod=futureis passed to thereverse()function which tries to match a pattern that excepts those keyword arguments – which you haven’t outlined)Edit:
A more appropriate pattern to use in your url would be something like:
where the selection could only be ‘past’ or ‘future’. This is fine for incoming urls, but django’s
reverse()function (which is used in the url template tag) can’t handle alternative choices:https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse