Django Вход пользователя по умолчанию: Невозможно войти в систему даже с правильным паролем
Я использую стандартный пользователь Django и аутентификацию для включения функции входа в систему. Мои имя пользователя и пароли верны; я проверил их, и я могу войти в админку. Но это не работает в моем приложении, через страницу входа. Должно быть, есть что-то основное, что я упускаю из виду, но я просмотрел свой код несколько раз.
Views.py:
Urls.py:
from django.urls import path
from django.contrib import admin
from . import views
urlpatterns = [
path("", views.EntryView.as_view(), name="index"),
path("entries/<int:pk>/delete", views.SingleEntryDeleteView.as_view(), name="single-entry-delete"),
path("entries/<int:pk>/", views.SingleEntryView, name="single-entry"),
path("register/", views.register, name="register"),
path("login/", views.login_view, name="login"),
path("logout/", views.user_logout, name="logout"),
path("admin/", admin.site.urls)
]
Forms.py:
from django import forms
from .models import Entry
# Authentication modules start here
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from django.forms.widgets import PasswordInput, TextInput
# Form to create a journal entry
class EntryForm(forms.ModelForm):
class Meta:
model=Entry
exclude = ["trader"]
# Create / register a user
class CreateUserForm(UserCreationForm):
class Meta:
model = User #default django User model
fields = ['username', 'email', 'password']
# Authenticate a user
class LoginForm(AuthenticationForm):
username = forms.CharField(widget=TextInput())
password = forms.CharField(widget=PasswordInput())
Models.py:
from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from django.utils.text import slugify
from django.contrib.auth.models import User
class Entry(models.Model):
ONEORB="1-Min ORB"
FIVEORB="5-Min ORB"
ABCD="ABCD"
REVERSAL="Reversal"
PROFIT="Profit"
LOSS="Loss"
RESULT_CHOICES = (
(PROFIT, "Profit"),
(LOSS, "Loss")
)
STRATEGY_CHOICES = (
(ONEORB,"1-Min ORB"),
(FIVEORB,"5-Min ORB"),
(ABCD,"ABCD"),
(REVERSAL,"Reversal")
)
entered_date=models.DateField(auto_now_add=True)
ticker=models.CharField(max_length=8, default="")
strategy=models.CharField(max_length=12, choices=STRATEGY_CHOICES, default="ONEORB")
result=models.CharField(max_length=6, choices=RESULT_CHOICES, default="PROFIT")
comments=models.TextField(max_length=300, blank=True)
image = models.ImageField(upload_to="", null=True, blank=True) #will save to default BASE_DIR which is 'uploads'
trader = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="entries")
def __str__(self):
return f"{self.result} {self.entered_date}"
Login.html:
{% extends 'base.html' %}
{% load static %}
{% block title %}
Trading Journal - Login
{% endblock title %}
{% block content %}
<h1>Sign-In to Your Account</h1>
<!-- Check for any messages -->
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<form method="POST" autocomplete="off">
{% csrf_token %}
{{ loginform.as_p }}
<button type="submit" value="sign-in">Sign In</button>
</form>
<h2>Don't have an account?</h2>
<a href="{% url 'register' %}"><button>Register</button></a>
{% endblock content %}