9

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?!

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hoo damned sorry, in fact my error was in my regex, I forgot "?" as [-2] char... 'schedule/(?P<calendar_id>\d+)/(?:month(?P<relative_month>[\+,\-]\d)/)?$' So I don't need multiple entries.
(As long as I sepcify all args in template even to None)
2

Has to be this ticket:

https://code.djangoproject.com/ticket/9176

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.