繁体   English   中英

Django如果user.is_authenticated不起作用

[英]Django if user.is_authenticated not working

我只是想尝试运行一个简单的{% if user.is_authenticated %} 但它总是返回False

这是我的所有文件。

views.py

from django.shortcuts import render_to_response, redirect
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from django.http import HttpResponse
from django.contrib.auth.hashers import *
from forms import UserLoginForm
from models import UserLogin

def index(request):
    return render_to_response('index.html', context_instance = RequestContext(request))

def login(request):
    if request.method == 'POST':
        form = UserLoginForm(request.POST or None)
        if form.is_valid():
            email_from_form = form.cleaned_data['email']
            password_from_form = form.cleaned_data['password']
            users = User.objects.filter(email = email_from_form)

            for j in users:
                if j.email == email_from_form:
                    pass_match = check_password(password_from_form, j.password)
                    if pass_match:
                        return redirect(reverse('profile'), context_instance = RequestContext(request))
                    else:
                        return HttpResponse('Entered password did not match')
                else:
                    return HttpResponse('Entered email does not exist')
        else:
            return HttpResponse(form.errors)
    else:
        form = UserLoginForm()

    return render_to_response('login.html', {'form':form}, context_instance = RequestContext(request))


def profile(request):

    return render_to_response('profile.html', context_instance = RequestContext(request))

forms.py

from django import forms

class UserLoginForm(forms.Form):
    email = forms.EmailField(max_length = 100)
    password = forms.CharField(max_length = 100)

models.py

from django.db import models

class UserLogin(models.Model):
    email = models.EmailField(max_length = 100)
    password = models.CharField(max_length = 100)

    def __unicode__(self):
        return self.email

profile.html

<html>
<head>
    <title>profile</title>
</head>
<body>
    {% if user.is_authenticated %}
        Welcome user, you are loged in..
    {% else %}
        You are not logged in
    {% endif %}
</body>
</html>

login.html

<html>
<head>
    <title>login</title>
</head>
<body>
    <form action="{% url login %}" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" id="submit" name="submit" value="Login" />
    </form>

</body>
</html>

它总是返回You are not logged in

我是django的新手,我不明白为什么会这样。

您永远不会记录您的用户。请尝试以下几行:

from django.contrib.auth import authenticate, login as auth_login

if request.method == 'POST':
    form = UserLoginForm(request.POST or None)
    if form.is_valid():
        username = User.objects.get(email=form.cleaned_data['email'])
        password = form.cleaned_data['password']
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                auth_login(request, user)
                return HttpResponseRedirect(request.GET.get('next',
                                            settings.LOGIN_REDIRECT_URL))
        else:
            error = 'Invalid username or password.'

更新以使用您的表单清晰; 错误检查排除在外

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM