最近在学django,不过遇到一个奇怪的问题:
登陆成功后,无法正确显示登陆后的view
这个是我的login view:
def login(request):
"""
View of for users' logging
"""
if request.method == 'POST':
userform = UserForm(request.POST)
if userform.is_valid():
username = userform.cleaned_data['username']
password = userform.cleaned_data['password']
confirm = UserProfile.objects.filter(username__exact=username, password__exact=password)
positive = HttpResponseRedirect(reverse('accounts:index'))
negative = HttpResponseRedirect(reverse('accounts'))
return positive if confirm else negative
else:
userform = UserForm()
dict_pass = {'userform': userform}
return render(request, 'accounts/login.html', dict_pass)
认证通过后应该登陆的view:
@login_required
def index(request):
dict_pass = {}
return render(request, 'accounts/index.html', dict_pass)
如果不用装饰,那么是可以正常跳转的,但是用了装饰器就无法正常跳转到index页面了,用户名密码也没有输错,包导入也没有问题。
问之前我查了下相关的话题,没有人遇到过这种问题啊,都是直接吧login_required放上去就可以了,举的例子也都是这样的,这就奇怪了。
@login_required 문서와 소스 코드를 살펴보세요: https://docs.djangoproject.co...
간단히 말하면 @login_required 인증은 request.user.is_authenticated입니다. 공식 예제에서는 세션 세션을 사용하며, 뷰에 로그인할 때 로그인 정보가 세션에 채워졌습니다.
로그인 성공 후
django.contrib.auth.login(request, user)
를 사용하여 사용자를 세션에 저장하세요