在Django注销内置视图中添加extra_context [英] Adding extra_context in Django logout built-in view
问题描述
在 django / contrib / auth / views.py
有注销视图的定义:
In django/contrib/auth/views.py
there is the definition of the logout view :
def logout(request, next_page=None,
template_name='registration/logged_out.html',
redirect_field_name=REDIRECT_FIELD_NAME,
current_app=None, extra_context=None):
我想添加extra_context以摆脱我注销时出现的登出标题
I would like to add extra_context to get rid of the 'Logged out' title that appear when I log off
所以我试图在我的url confs:
so I'm trying this in my url confs :
(r'^accounts/logout/$', logout(extra_context={'title':'something else'}) ),
但是我得到这个错误:logout()至少需要1个非关键字参数(0给定)
我做错了什么?
ps:
当我做
but then I get this error : logout() takes at least 1 non-keyword argument (0 given) what I'm doing wrong? ps: when I do
(r'^accounts/logout/$', logout ),
它可以工作,但后来我收到了注销文本...
it works, but then I get the 'Logged out' text...
谢谢,
Fred
Thanks, Fred
推荐答案
当你写 logout(extra_context = {'title':'something else'})
,你实际上是在URLconf中调用 logout
这不行。 任何URLconf元组都可以有一个可选的第三个元素,其中应该是一个额外的关键字参数的字典传递给视图功能。
When you write logout(extra_context={'title':'something else'})
, you're actually calling logout
right there in the URLconf, which won't work. Any URLconf tuple can have an optional third element, which should be a dictionary of extra keyword arguments to pass to the view function.
(r'^accounts/logout/$', logout, {'extra_context':{'title':'something else'}}),
或者,您可以编写自己的视图,调用注销
传入你想要的任何参数 - 这通常是在更复杂的情况下扩展基于功能的通用视图。
Alternatively, you could write your own view which calls logout
passing in whatever arguments you want -- that's typically how you would "extend" function-based generic views in more complicated cases.
这篇关于在Django注销内置视图中添加extra_context的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!