使用通用PasswordChangeForm Django 视图渲染时捕获NoReverseMatch
我试图在 Django 中使用通用的PasswordChangeForm 和password_change 视图,并遇到NoReverseMatch 错误。
这是我的 urls.py:
(r'^change_pass/','django.contrib.auth.views.password_change', {'post_change_redirect':'/home'}),
然后,我尝试从模板中调用以下内容:
<form action="{% url django.contrib.auth.views.password_change post_change_redirect="/home/userpage" %}" method="POST">
渲染时捕获
NoReverseMatch:使用参数 '()' 和关键字参数反转 'django.contrib.auth.views.password_change'找不到“{'post_change_redirect':u'/home/userpage'}”。
我做错了什么?我认为如果在 urls.py 中声明了一个可选变量,那么您可以在调用时通过将其指定为命名参数来覆盖它。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您定义 URL 的方式,
post_change_redirect
参数不是可选的 - 事实上它根本不被接受。视图始终获取您在 urlconf 中定义的硬编码值。因此,当{% url %}
标记寻找接受post_change_redirect
参数的 password_change 视图时,它找不到,因为您还没有定义该视图。如果您考虑一下定义 URL 的方式,如果调用者想要覆盖它,参数会去哪里?
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 apost_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?