views:

185

answers:

1

I'm trying to use the generic PasswordChangeForm and password_change view in Django, and running into a NoReverseMatch error.

Here is my urls.py:

    (r'^change_pass/','django.contrib.auth.views.password_change', {'post_change_redirect':'/home'}),

Then, I'm trying to call the following from the template:

<form action="{% url django.contrib.auth.views.password_change post_change_redirect="/home/userpage" %}" method="POST">

I get

Caught NoReverseMatch while rendering: Reverse for 'django.contrib.auth.views.password_change' with arguments '()' and keyword arguments '{'post_change_redirect': u'/home/userpage'}' not found.

What am I doing wrong? I thought if an optional variable was declared in the urls.py, you could then override it when calling by specifying it as a named argument. Thanks!

A: 

The way you have defined the URL, the post_change_redirect parameter is not optional - in fact it is not accepted at all. The view always get the hard-coded value you have defined in the urlconf. So when the {% url %} tag goes looking for a password_change view that accepts a post_change_redirect parameter, it can't find one, because you haven't defined one.

If you think about the way you have defined the URL, where would the argument go if a caller wanted to override it?

Daniel Roseman
So the only way to do a post_change_redirect in this case is to pass it right into the URL using (?P<post_change_redirect> [whatever]) -- doesn't that get ugly when I'm passing things that look like URLs themselves?
Andrew
I ended up reimplementing it so that it takes the next out of the REQUEST -- seems like that way is a bit more consistent. Or is there a better way that you could think of?
Andrew