Python Django登录脚本不断返回无效请求

Python Django登录脚本不断返回无效请求,python,html,django,Python,Html,Django,嗨,我希望有人能帮忙 我是Django新手,一直在按照教程制作登录脚本。我创建了一个脚本,允许用户从HTML表单使用postgres中的users表登录。尽管用户凭据正确,登录脚本始终返回“无效用户”。我已经遵循了许多故障排除指南,现在我已经筋疲力尽了,这真是令人沮丧。请参阅下面我的代码中的不同部分。任何帮助都将不胜感激 Account\Views.py def login_view(request, *args, **kwargs): context = {} user

嗨,我希望有人能帮忙

我是Django新手,一直在按照教程制作登录脚本。我创建了一个脚本,允许用户从HTML表单使用postgres中的users表登录。尽管用户凭据正确,登录脚本始终返回“无效用户”。我已经遵循了许多故障排除指南,现在我已经筋疲力尽了,这真是令人沮丧。请参阅下面我的代码中的不同部分。任何帮助都将不胜感激

Account\Views.py 

def login_view(request, *args, **kwargs):
    context = {}

    user = request.user
    if user.is_authenticated:
        return redirect("home")

    destination = get_redirect_if_exists(request)
    print("destination: " + str(destination))

    if request.POST:
        form = AccountAuthenticationForm(request.POST)
        if form.is_valid():
            email = request.POST['email']
            password = request.POST['password']
            user = authenticate(email=email, password=password)

            if user:
                login(request, user)
                if destination:
                    return redirect(destination)
                return redirect("home")

    else:
        form = AccountAuthenticationForm()

    context['login_form'] = form

    return render(request, "account/login.html", context)


def get_redirect_if_exists(request):
    redirect = None
    if request.GET:
        if request.GET.get("next"):
            redirect = str(request.GET.get("next"))
    return redirect
Account\forms.py 
class AccountAuthenticationForm(forms.ModelForm):

    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    class Meta:
        model = Account
        fields = ('email', 'password')

    def clean(self):
        if self.is_valid():
            email = self.cleaned_data['email']
            password = self.cleaned_data['password']
            if not authenticate(email=email, password=password):
                raise forms.ValidationError("Invalid login")
urls.py

from account.views import(
    register_view,
    login_view,
    logout_view,
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home_screen_view, name='home'),
    path('register/', register_view, name='register'),
    path('login/', login_view, name="login"),
    path('logout/', logout_view, name="logout"),
]

Login Form

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="card card-signin">
          <div class="card-body">
            <form class="form-signin" method="post">{% csrf_token %}

                <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>

                <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required>

                {% for field in login_form %}
                <p>
                {% for error in field.errors %}
                <p style="color: red">{{ error }}</p>
                {% endfor %}
                </p>
                {% endfor %}
                {% if login_form.non_field_errors %}
                <div style="color: red">
                <p>{{login_form.non_field_errors}}</p>
                </div>

                {% endif %}

                <button class="btn btn-lg btn-primary btn-block" type="submit">Log in</button>
                </form>

                <div class="d-flex flex-column my-4">
                    <a class="m-auto" href="#">Reset password</a>
                </div>
          </div>
        </div>

    </div>
</div>
Account\model.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.files.storage import FileSystemStorage
from django.conf import settings



class MyAccountManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError('Users must have an email address')
        if not username:
            raise ValueError('Users must have a username')

        user = self.model(
            email=self.normalize_email(email),
            username=username,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            username=username,
        )
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user

def get_profile_image_filepath(self, filename):
    return 'profile_images/' + str(self.pk) + '/profile_image.png'

def get_default_profile_image():
    return "images/logo_1080_1080.png"

class Account(AbstractBaseUser):
    email = models.EmailField(verbose_name="email", max_length=60, unique=True)
    username = models.CharField(max_length=30, unique=True)
    date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
    last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    objects = MyAccountManager()

    def __str__(self):
        return self.username


    # For checking permissions. to keep it simple all admin have ALL permissions
    def has_perm(self, perm, obj=None):
        return self.is_admin

    # Does this user have permission to view this app? (ALWAYS YES FOR SIMPLICITY)
    def has_module_perms(self, app_label):
        return True