views:

362

answers:

1

Folks, I am getting a NoReverseMatch error for a particular url call.

I'd like to know: are there any good tools to debug these in general? For example, some way to list out which URLs are registered?

My particular example:

Template:

<a href=
"{% url django.contrib.auth.views.redirect_to_login blarg %}">log in</a>

Error:

NoReverseMatch: Reverse for
 'settings.django.contrib.auth.views.redirect_to_login'
 with arguments '('[[ UNDEFINED VARIABLE ]]',)'
 and keyword arguments '{}' not found.

I am using Google App Engine with appenginepatch, so Django itself is modified.

+2  A: 

In this particular case where the url reversal uses the full path to the view function, the easy thing to do is just go look at the view function. In Django-1.1 this looks like:

def redirect_to_login(next, login_url=None,
                            redirect_field_name=REDIRECT_FIELD_NAME):
    "Redirects the user to the login page, passing the given 'next' page"
    if not login_url:
        login_url = settings.LOGIN_URL
    return HttpResponseRedirect('%s?%s=%s' % (login_url,
                                              urlquote(redirect_field_name), 
                                              urlquote(next)))

This function does not even take a request object, so its really not even a proper view, and it is not even registered in django/contrib/auth/urls.py. That means it's probably only meant to be used as a helper function in other views.

In any case, based on your particular example, what you probably wanted to do was use the plain old login url like so:

<a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">
  log in
</a>

Also, I believe if you set TEMPLATE_DEBUG = True in your settings, you will get a list of all the url patterns django checked against before throwing the error.

pcardune
Thanks! Well described. This is basically what I ended up doing, using auth.REDIRECT_FIELD_NAME in place of "next".However, the TEMPLATE_DEBUG=True did not show any further information about URL patterns, so I am leaving this question open.Since I am using Google App Engine with app engine patch, Django is pretty patched, so I wanted to see the actual URL patterns registered, not just guess at it.
dfrankow
I know I've seen that debug page running in google app engine as well, but I was just using the built in django and not the app engine patch.
pcardune
Yeah, if you set DEBUG=True you see that table of patterns some times and not others. At the moment I forget why it doesn't always come up.
dfrankow