在我的urls.py中,我将url accounts / login /映射到登录模块,并引用了我想要使用的模板:
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'templates/login.html'})
这很好用,但我想更改next
属性的值,该属性指定用户在成功登录后应该重定向的位置。如何访问这些变量并打印它们的值,更重要的是,我如何修改它们?
谢谢!
答案 0 :(得分:1)
将网址指向视图:
(r'^accounts/login/', 'myproj.login.views.mylogin')
然后在视图代码中处理重定向:
def mylogin(request, **kwargs):
if request.user.is_authenticated():
if 'next_url' in request.session:
url = request.session['next_url']
del request.session['next_url'] # Cleaning next_url val
return HttpResponseRedirect('/%s' % url)
else:
return HttpResponseRedirect('/')
return login(request, **kwargs)
@csrf_protect
def login(request, template_name='registration/login.html'):
"""Displays the login form and handles the login action."""
retval = django.contrib.auth.views.login(request, template_name)
clear_session_data(request.session)
return retval