How to reverse url with optional fields arguments in django?
I have an url with an optionnal argument:
urlpatterns = patterns(
'my_app.views',
url('schedule/(?P<calendar_id>\d+)/(?:month(?P<relative_month>[\+,\-]\d)/)$',
'attribute_event',name='attribute_event')
)
In my template I have a link:
{% url attribute_event calendar.id %}
But I have an error saying开发者_运维技巧 the url can't be reversed with these args. Must I use 2 url regex entry and url names?!
only possible if you split it into two urls:
urlpatterns = patterns('my_app.views',
url('schedule/(?P<calendar_id>\d+)/month(?P<relative_month>[\+,\-]\d)/$',
'attribute_event', name='attribute_event_relative'),
url('schedule/(?P<calendar_id>\d+)/)$',
'attribute_event', name='attribute_event'),
)
in template:
{% url attribute_event calendar.id %}
or
{% url attribute_event_relative calendar.id '+1' %}
your view:
def attribute_event(request, calendar_id, relative_month=None):
pass
Has to be this ticket:
https://code.djangoproject.com/ticket/9176
精彩评论