@login_required没有做任何事情

时间:2016-07-21 05:30:49

标签: django

似乎@ login_required装饰师对我没有任何作用。如果用户未登录并呼叫注册,我希望它重定向到登录页面。我错过了什么。我甚至试过设置@login_required(login_url='/login/'),但它没有成功。

我确保设置LOGIN_URLLOGIN_URL_REDIRECTINSTALLED_APPS和middleware_classes中包含所有必需参数。我正在使用最新的django 1.9

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        username = request.POST['username']
        password = request.POST['password']
        print "u %s p:%s" % (username, password)
        user = authenticate(username=username, password=password)
        print "Authentication done"
        if user is not None:
            print "logging in"
            login(request, user)
            return HttpResponseRedirect(request.POST['next'])
        else:
            print("The username and password were incorrect.")
    else:
        form = LoginForm()
    return render(request, 'login.html', {'loginForm': form})


@login_required
def registration(request):
    dosomething...

class LoginForm(forms.ModelForm):
    class Meta:
        model = User  

1 个答案:

答案 0 :(得分:3)

清除所有浏览器缓存对我有用。谢谢大家的支持