I was getting a TemplateSyntaxError? caused by a NoReverseMatch? error with DJango 1.0 for code which previously worked.
Exception Type: TemplateSyntaxError
Exception Value:
Caught an exception while rendering: Reverse for 'x.x_edit' with arguments '(7,)' and keyword arguments '{}' not found.
Original Traceback (most recent call last):
File "/usr/local/lib/python2.5/site-packages/django/template/debug.py", line 71, in render_node
result = node.render(context)
File "/usr/local/lib/python2.5/site-packages/django/template/defaulttags.py", line 378, in render
args=args, kwargs=kwargs)
File "/usr/local/lib/python2.5/site-packages/django/core/urlresolvers.py", line 252, in reverse
*args, **kwargs)))
File "/usr/local/lib/python2.5/site-packages/django/core/urlresolvers.py", line 241, in reverse
"arguments '%s' not found." % (lookup_view, args, kwargs))
NoReverseMatch: Reverse for 'x.x_edit' with arguments '(7,)' and keyword arguments '{}' not found.
Exception Location: /usr/local/lib/python2.5/site-packages/django/template/debug.py in render_node, line 81
I eventually tracked it down to this:
I was using a url template tag to match the name 'edit' (see below). The pattern was the same as a preceding pattern which matched the same prefix with an optional number after it. This first url pattern had no name (I'd originally found I needed the 2 patterns because the first pattern had 2 slashes at the end and didn't match the one slash I wanted).
url(r'^x/edit/(?P<x_id>[0-9]*)/$', x_edit,),
url(r'^x/edit/$', x_edit, name='x_edit'),
The snippet of template code was:
<form action="{% url x_edit x.id %}/" method="post" name="my_form">
This used to work (presumably by failing the named match and somehow matching the un-named one?).
However, in 1.0 it stopped working (I guess because the url resolver has been re-worked).
I found (after much debugging), that I could fix the problem by adding another name for the 1st pattern, eg
url(r'^x/edit/(?P<x_id>[0-9]*)/$', x_edit, name='x_edit_id'),
url(r'^x/edit/$', x_edit, name='x_edit'),
and changing my template to use it:
<form action="{% url x_edit_id x.id %}/" method="post" name="my_form">
This took quite a while for me to find, and I'd like peole to not have to do the same as I have in migrating from older code to 1.0!
Can you maybe confirm the above and document this somewhere sensible for people who are migrating from 0.96+ to 1.0 (I think this was probably a backwards-incompatible change)?
Thanks!
Mary.