Monday, 15 March 2010

Django template url tag with single parameter for url with ORed capture group fails on reverse lookup -



Django template url tag with single parameter for url with ORed capture group fails on reverse lookup -

not best title.. anyway: have next urlconf entry:

url(r'^events(?:/(?:(?p<event_id>\d+)|(?p<new>new)))?/?$', eventview.as_view(), name='events')

which can see, can take no path components after events, or number or word 'new'. problem when trying reverse lookup 1 of 2 capture groups within template. django doesn't seem realize if template url tag given single argument it's ok since regex says it's either 1 grouping or other.

for instance, want go for:

/events/1

i'm trying this:

{% url 'social_network:events' event.id %}

and get:

reverse 'events' arguments '(1,)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['events(?:/(?:(?p<event_id>\\d+)|(?p<new>new)))?/?$']

i tried named parameters (event_id=1 instead) have parameters passed in dictionary still same error. mind you, if remove 1 of ored capture groups, resolves fine.

any thoughts?

django can't reverse urls optional capture groups, because can't know set there. best way deal situation have multiple entries in urlconf: 1 no-param version, 1 id, , 1 new.

you can still have them pointing @ same view - although must since behaviour different 3 cases, code clearer if had 3 separate views.

django django-templates

No comments:

Post a Comment