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

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

天蓬老师
天蓬老师

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

全部回覆(2)
Ty80

看一下@login_required文件及原始碼: https://docs.djangoproject.co...

簡單提下,@login_required驗證是request.user.is_authenticated。官方的例子是應用session會話的,login view時已經將登入資訊塞到session中。

黄舟

登入成功後要用django.contrib.auth.login(request, user)把user存入session

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!