Weird problem in url resolving after restarting django dev. server

19 views
Skip to first unread message

Konstantin S

unread,
May 20, 2009, 6:02:04 AM5/20/09
to Django users
Hello!

I have a very strange problem and really don't know where to start in
hunting it down. My app uses django-registration, all works fine but
if I been logged in restart django dev. server I immediately get
TemplateSyntaxError:

Caught an exception while rendering: Reverse for
'myapp.add_media_action' with arguments '()' and keyword arguments
'{}' not found.

It seems like after restarting django couldn't resolve url's names. I
know this is a very vague and incomplete description but may be it is
a very well known problem that you can identify on the spot ?

Konstantin S

unread,
May 20, 2009, 7:28:05 AM5/20/09
to Django users
I've tried it in a django shell. 'myapp.add_media_action' really does
not resolve, but 'add_media_action' resolves fine. That makes it even
more puzzling. Why am I getting 'myapp.add_media_action' instead of
'add_media_action' ?

Karen Tracey

unread,
May 20, 2009, 9:41:56 AM5/20/09
to django...@googlegroups.com

That's a misleading message. The code tries to reverse the requested name with the project  name prepended only after attempting to reverse the requested name on its own has failed.  See:

http://code.djangoproject.com/browser/django/trunk/django/template/defaulttags.py#L364

That code was recently (r10350, on April 1st) fixed so that the original NoReverseMatch is raised instead of the one resulting from the last-ditch attempt to find a reversal. 

So, ignore the 'myapp' part of the message.  The real issue is that your server can't reverse 'add_media_action', even though you can reverse it from the shell.  There's got to be some difference between your server environment and the shell environment you are using that is causing that.

Karen

Konstantin S

unread,
May 20, 2009, 10:09:21 AM5/20/09
to Django users
On 20 май, 17:41, Karen Tracey <kmtra...@gmail.com> wrote:
> There's got to be some difference between your server
> environment and the shell environment you are using that is causing that.
>

All works fine until I restart django dev. http server. If at that
moment I was logged in into my site then next hit after restarting
would cause that NoReverseMatch error. May it be a problem that almost
all my views wrapped into login_required decorator like that: url
(r'^add/$', login_required(views.add_media), name =
'add_media_action'). And if it so how to fix it ?

Steve Howell

unread,
May 20, 2009, 11:43:59 AM5/20/09
to Django users
On May 20, 6:41 am, Karen Tracey <kmtra...@gmail.com> wrote:
> On Wed, May 20, 2009 at 7:28 AM, Konstantin S <ktechli...@gmail.com> wrote:
>
> > On 20 май, 14:02, Konstantin S <ktechli...@gmail.com> wrote:
> > > Hello!
>
> > > I have a very strange problem and really don't know where to start in
> > > hunting it down. My app uses django-registration, all works fine but
> > > if I been logged in restart django dev. server I immediately get
> > > TemplateSyntaxError:
>
> > > Caught an exception while rendering: Reverse for
> > > 'myapp.add_media_action' with arguments '()' and keyword arguments
> > > '{}' not found.
>
> > > It seems like after restarting django couldn't resolve url's names. I
> > > know this is a very vague and incomplete description but may be it is
> > > a very well known problem that you can identify on the spot ?
>
> > I've tried it in a django shell.  'myapp.add_media_action' really does
> > not resolve, but 'add_media_action' resolves fine. That makes it even
> > more puzzling. Why am I getting 'myapp.add_media_action' instead of
> > 'add_media_action' ?
>
> That's a misleading message. The code tries to reverse the requested name
> with the project  name prepended only after attempting to reverse the
> requested name on its own has failed.  See:
>
> http://code.djangoproject.com/browser/django/trunk/django/template/de...
>
> That code was recently (r10350, on April 1st) fixed so that the original
> NoReverseMatch is raised instead of the one resulting from the last-ditch
> attempt to find a reversal.
>
> So, ignore the 'myapp' part of the message.  The real issue is that your
> server can't reverse 'add_media_action', even though you can reverse it from
> the shell.  There's got to be some difference between your server
> environment and the shell environment you are using that is causing that.
>

Konstantin,

To elaborate on Karen's suggestion, one way to see which URLs are
among the candidates for resolving the reverse match is to
deliberately hit a bad URL like the following:

http://localhost:8000/something_that_does_not_match_urls

If you have appropriate debug settings, you should then get back a
list of all the patterns that django knows about in your browser. If
you don't see the URL for add_media_action, then you probably have
some issue with the environment, as Karen suggests.

If you do see the URL for add_media_action, then you probably want to
focus on other theories, such as the wrong number of arguments or
something similar.

Obviously, more information would help people help you figure this
out. At the very least you should provide the line of code from
urls.py that establishes the mapping for add_media_action.

Konstantin S

unread,
May 20, 2009, 1:49:42 PM5/20/09
to Django users
On 20 май, 19:43, Steve Howell <showel...@yahoo.com> wrote:
>
> To elaborate on Karen's suggestion, one way to see which URLs are
> among the candidates for resolving the reverse match is to
> deliberately hit a bad URL like the following:
>
> http://localhost:8000/something_that_does_not_match_urls
>
> If you have appropriate debug settings, you should then get back a
> list of all the patterns that django knows about in your browser.  If
> you don't see the URL for add_media_action, then you probably have
> some issue with the environment, as Karen suggests.

Yes, there is URL for 'add_media_action'. As I said before all works
as expected till I restart dev. server.

>
> If you do see the URL for add_media_action, then you probably want to
> focus on other theories, such as the wrong number of arguments or
> something similar.
>

I've checked it several times and found nothing wrong. Again, problems
occur just after I restart dev. server, before that moment I don't get
any errors. That's why I am so frustrated with this problem

> Obviously, more information would help people help you figure this
> out.  At the very least you should provide the line of code from
> urls.py that establishes the mapping for add_media_action.

It's very simple (from myapp/urls.py)

url(r'^add/$', login_required(views.add_media),
name = 'add_media_action'),

And my project urls.py looks like this:

urlpatterns = patterns('',
url(r'^$', 'views.index', name="index"),
(r'^data/', include('myapp.urls')),
(r'^accounts/', include('registration.urls')),
(r'^admin/(.*)', admin.site.root),
url(r'^ajax/autocomplete/$', 'views.ajax_autocomplete_action',
name='ajax_autocomplete_action'),
url(r'^about/$', 'django.views.generic.simple.direct_to_template',
{'template': 'misc/about.html'}, name="about"),
)

Steve Howell

unread,
May 20, 2009, 2:48:56 PM5/20/09
to Django users
On May 20, 10:49 am, Konstantin S <ktechli...@gmail.com> wrote:
> On 20 май, 19:43, Steve Howell <showel...@yahoo.com> wrote:
>
>
>
> > To elaborate on Karen's suggestion, one way to see which URLs are
> > among the candidates for resolving the reverse match is to
> > deliberately hit a bad URL like the following:
>
> >http://localhost:8000/something_that_does_not_match_urls
>
> > If you have appropriate debug settings, you should then get back a
> > list of all the patterns that django knows about in your browser.  If
> > you don't see the URL for add_media_action, then you probably have
> > some issue with the environment, as Karen suggests.
>
> Yes, there is URL for 'add_media_action'. As I said before all works
> as expected till I restart dev. server.
>

Just to be clear, I'm suggesting that you try the technique above
after you restart the dev server.

Konstantin S

unread,
May 20, 2009, 3:20:03 PM5/20/09
to Django users
Yes, of course I tried it after restart the dev. server

Konstantin S

unread,
May 26, 2009, 9:43:32 AM5/26/09
to Django users
Continue my story...

I've changed a bit template code:

was: <li><a href="{% url add_media_action %}">Add new</a></li>
now: <li><a href="{% url myapp.views.add_media %}">Add new</a></li>

Error message also changed, now it is:

Caught an exception while rendering: Reverse for
'<django.contrib.auth.decorators._CheckLogin object at 0x235aa50>'
with arguments '()' and keyword arguments '{}' not found.

I have no idea how to fix this problem, please help if you can.

Steve Howell

unread,
May 26, 2009, 1:28:41 PM5/26/09
to Django users
From the docs you might want to take advantage of the name= parameter
in your URLS setup and do something like {% url 'add_media_action'
%}. Not sure about the quoting, as the docs are vague about quoting
literals.

'''
New in Django 1.0: Please, see the release notes

If you're using named URL patterns, you can refer to the name of the
pattern in the url tag instead of using the path to the view.
'''

It seems like the login_required method may be confusing you:

url(r'^add/$', login_required(views.add_media),
name = 'add_media_action'),

I am guessing the login_required method returns another method that
gets set up into the url data structure, so the reverse code has no
way of knowing about views.add_media anymore. Does that make sense
to you?

Perhaps you can try to step through the code in the debugger. The top-
level method involved in reversing URLs are not super complicated.
You can set a breakpoint in django/core/urlresolver.py to see what's
happening.

Look for this snippet of code (which may be slightly different for
your version of django, but I doubt it's changed too much):

def reverse(self, lookup_view, *args, **kwargs):
if args and kwargs:
raise ValueError("Don't mix *args and **kwargs in call to
reverse()!")
try:
lookup_view = get_callable(lookup_view, True)
except (ImportError, AttributeError), e:
raise NoReverseMatch("Error importing '%s': %s." %
(lookup_view, e))
possibilities = self.reverse_dict.getlist(lookup_view)
for possibility, pattern in possibilities:
for result, params in possibility:
if args:
if len(args) != len(params):
continue
unicode_args = [force_unicode(val) for val in
args]
candidate = result % dict(zip(params,
unicode_args))
else:
if set(kwargs.keys()) != set(params):
continue
unicode_kwargs = dict([(k, force_unicode(v)) for
(k, v) in kwargs.items()])
candidate = result % unicode_kwargs
if re.search(u'^%s' % pattern, candidate, re.UNICODE):
return candidate
raise NoReverseMatch("Reverse for '%s' with arguments '%s' and
keyword "
"arguments '%s' not found." % (lookup_view, args,
kwargs))

Konstantin S

unread,
May 27, 2009, 11:23:15 AM5/27/09
to Django users
On May 26, 9:28 pm, Steve Howell <showel...@yahoo.com> wrote:
>
> From the docs you might want to take advantage of the name= parameter
> in your URLS setup and do something like {% url 'add_media_action'
> %}.  Not sure about the quoting, as the docs are vague about quoting
> literals.
>
> '''
> New in Django 1.0: Please, see the release notes
>
> If you're using named URL patterns, you can refer to the name of the
> pattern in the url tag instead of using the path to the view.
> '''
>
> It seems like the login_required method may be confusing you:
>
> url(r'^add/$', login_required(views.add_media),
>         name = 'add_media_action'),
>
> I am guessing the login_required method returns another method that
> gets set up into the url data structure, so the reverse code has no
> way of knowing about views.add_media anymore.   Does that make sense
> to you?
>
> Perhaps you can try to step through the code in the debugger.  The top-
> level method involved in reversing URLs are not super complicated.
> You can set a breakpoint in django/core/urlresolver.py to see what's
> happening.

Steve, thanks for your suggestion to trace django code, I've finally
found what was the problem. Don't know how to fix it yet so I disabled
some fancy ajax-based functionality. But at least I am no getting this
mysterious 500 errors any more.

Steve Howell

unread,
May 27, 2009, 12:11:27 PM5/27/09
to Django users

On May 27, 8:23 am, Konstantin S <ktechli...@gmail.com> wrote:
> On May 26, 9:28 pm, Steve Howell <showel...@yahoo.com> wrote:
>
> [...]
> > Perhaps you can try to step through the code in the debugger.  The top-
> > level method involved in reversing URLs are not super complicated.
> > You can set a breakpoint in django/core/urlresolver.py to see what's
> > happening.
>
> Steve, thanks for your suggestion to trace django code, I've finally
> found what was the problem. Don't know how to fix it yet so I disabled
> some fancy ajax-based functionality. But at least I am no getting this
> mysterious 500 errors any more.

You're welcome, glad you're making progress.

Here is a snippet I found related to debugging URL problems, just in
case others are watching this thread or it comes up in Google.

http://www.djangosnippets.org/snippets/1434/

Reply all
Reply to author
Forward
0 new messages