Django反向()问题

发布于 2024-10-26 21:42:31 字数 1269 浏览 5 评论 0原文

我在这里缺少一些非常基本的东西。

我正在尝试重用 django 的更改密码视图。我在 urls.py:

(r'^change-password/$', 'profile.views.change_password', {},'change_password'),
url(r'^change-password-done/$', 'profile.views.password_change_done', name='django.contrib.auth.views.password_change_done'),

和相应的views.py: 中

from django.contrib.auth.views import password_change, password_change_done

def change_password(request,template_name="password_change_form.html"):
    """Change Password"""
    return password_change(request,template_name=template_name)

def password_change_done(request, template_name="password_change_done.html"):
    return render_to_response(template_name,(),context_instance= RequestContext(request))

有以下内容,但出现以下错误:

反转为 'django.contrib.auth.views.password_change_done' 带参数“()”和关键字 未找到参数“{}”。

查看源代码并看到这一行:

post_change_redirect = reverse('django.contrib.auth.views.password_change_done')

如果我将 urls.py 条目更改为以下,我不会收到上述错误:

url(r'^change-password-done/$', 'django.contrib.auth.views.password_change_done', name='anything'),

但我很困惑,因为verse()应该使用“name”参数进行查找?我在这里缺少什么?

我正在使用 django 1.2.3

I am missing something really basic here.

I am trying to reuse django's change password views. I have following in urls.py:

(r'^change-password/

and in corresponding views.py:

from django.contrib.auth.views import password_change, password_change_done

def change_password(request,template_name="password_change_form.html"):
    """Change Password"""
    return password_change(request,template_name=template_name)

def password_change_done(request, template_name="password_change_done.html"):
    return render_to_response(template_name,(),context_instance= RequestContext(request))

but I am getting following error:

Reverse for
'django.contrib.auth.views.password_change_done'
with arguments '()' and keyword
arguments '{}' not found.

looked at the source and saw this line:

post_change_redirect = reverse('django.contrib.auth.views.password_change_done')

If I change my urls.py entry to following , I do not get the above error:

url(r'^change-password-done/

but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

, 'profile.views.change_password', {},'change_password'), url(r'^change-password-done/

and in corresponding views.py:


but I am getting following error:

Reverse for
'django.contrib.auth.views.password_change_done'
with arguments '()' and keyword
arguments '{}' not found.

looked at the source and saw this line:


If I change my urls.py entry to following , I do not get the above error:


but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

, 'profile.views.password_change_done', name='django.contrib.auth.views.password_change_done'),

and in corresponding views.py:


but I am getting following error:

Reverse for
'django.contrib.auth.views.password_change_done'
with arguments '()' and keyword
arguments '{}' not found.

looked at the source and saw this line:


If I change my urls.py entry to following , I do not get the above error:


but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

, 'django.contrib.auth.views.password_change_done', name='anything'),

but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

, 'profile.views.change_password', {},'change_password'), url(r'^change-password-done/

and in corresponding views.py:

but I am getting following error:

Reverse for
'django.contrib.auth.views.password_change_done'
with arguments '()' and keyword
arguments '{}' not found.

looked at the source and saw this line:

If I change my urls.py entry to following , I do not get the above error:

but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

, 'profile.views.password_change_done', name='django.contrib.auth.views.password_change_done'),

and in corresponding views.py:

but I am getting following error:

Reverse for
'django.contrib.auth.views.password_change_done'
with arguments '()' and keyword
arguments '{}' not found.

looked at the source and saw this line:

If I change my urls.py entry to following , I do not get the above error:

but I am confused as reverse() should look-up using the "name" parameter? What am I missing here?

I am using django 1.2.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-11-02 21:42:32

乔希有解释,但你做错了。如果您想覆盖 post_save_redirect,请在调用视图时将其作为参数传递:

def change_password(request,template_name="password_change_form.html"):
    return password_change(request, template_name=template_name, 
                           post_save_redirect=reverse('my_done_page'))

请参阅 文档

Josh has the explanation, but you are doing this wrong. If you want to overrride the post_save_redirect, then pass that in as a parameter when you call the view:

def change_password(request,template_name="password_change_form.html"):
    return password_change(request, template_name=template_name, 
                           post_save_redirect=reverse('my_done_page'))

See the documentation.

任谁 2024-11-02 21:42:32

reverse 函数不仅仅对名称进行查找。

反向文档

反向(视图名称,urlconf=无,args=无,kwargs=无,current_app=无)
viewname 是函数名称(函数引用,或者名称的字符串版本,如果您在 urlpatterns 中使用该形式)或 URL 模式名称。

因此,通过执行 reverse('django.contrib.auth.views.password_change_done'),它将在 urls.py 正则表达式中查找该视图名称,并回退到如果视图名称无法解析,则查找 name 关键字参数。

The reverse function doesn't just do lookups on name.

Reverse Documentation.

reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
viewname is either the function name (either a function reference, or the string version of the name, if you used that form in urlpatterns) or the URL pattern name.

So, by doing reverse('django.contrib.auth.views.password_change_done'), it will look up that view name within the urls.py regexes, and fallback to looking for the name keyword argument if the viewname doesn't resolve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文