python - 关于django的登陆成功后的view,使用login_required装饰后无法正常显示?
天蓬老师
天蓬老师 2017-04-18 10:31:34
0
2
857

最近在学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放上去就可以了,举的例子也都是这样的,这就奇怪了。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
Ty80

Look at the @login_required documentation and source code: https://docs.djangoproject.co...

Just to mention, @login_required verification is request.user.is_authenticated. The official example uses a session session, and the login information has been stuffed into the session when logging in to the view.

黄舟

After successful login, you need to use django.contrib.auth.login(request, user)to save the user into the session

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!