Home  >  Q&A  >  body text

python - 关于django的登陆成功后的view,使用login_required装饰后无法正常显示?

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

天蓬老师天蓬老师2741 days ago865

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:33:34

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:33:34

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

    reply
    0
  • Cancelreply