Я пытаюсь создать простую страницу входа, используя python и django. Моя регистрация / регистрация работает нормально, и я могу зарегистрировать пользователя. как только пользователь зарегистрирован, во время входа в систему с использованием имени пользователя и пароля пользователь не аутентифицируется. Я не могу узнать точную ошибку, может кто-нибудь, пожалуйста, помогите мне через это. Это мой models.py
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
# Create your models here.
# create a model for user details with fields username, firstname, lastname,
# emial, password,
class CustomUserInfo(AbstractBaseUser):
'''
model for basic user details
returns username of the user
'''
firstname = models.CharField(max_length=50)
lastname = models.CharField(max_length=50)
username = models.CharField(max_length=50, unique=True)
email = models.EmailField()
password = models.CharField(max_length=15)
dob = models.DateField()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['firstname','lastname','username','email','password','dob']
# return the string form of the object
def __str__(self):
return self.username
Это мой файл views.py
from django.shortcuts import render
from django.views.generic import TemplateView, DetailView, ListView
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.http import HttpResponse,HttpResponseRedirect
from custom_login_app.forms import CustomUserInfoForm
from custom_login_app import models
# Create your views here.
# about template view
class AboutView(TemplateView):
template_name = 'about.html'
# user profile detail page
class ProfileDetailView(DetailView):
context_object_name = 'user_detail'
model = models.CustomUserInfo
template_name = 'userinfo_detail.html'
# home page view
def index(request):
return render(request,'index.html')
# logout view
@login_required
def customlogout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))
# registration view
def registration(request):
# bool variable to chcek the user is registered
registered = False
# check the user has poste the data through the form
if request.method == 'POST':
# create a form object
user_form = CustomUserInfoForm(data=request.POST)
# check if the form is valid
if user_form.is_valid():
# save the entered details to the form object
user = user_form.save()
# validate and set the password for hashing
user.set_password(user.password)
# save the details
user.save()
registered = True
else:
print(user_form.errors)
else:
# display the registration form
user_form = CustomUserInfoForm()
return render(request,'registration.html',{'user_form':user_form,
'registered':registered})
# login view
def customlogin(request):
# check if the user has posted the details
if request.method == 'POST':
# Grab the entered username and password
username = request.POST.get('username')
password = request.POST.get('password')
# authenticate the entered details using built in django authenticate method
user = authenticate(username=username, password=password)
# if the user is authenticated, user is available
if user:
# check is the user active
if user.is_active:
# login the user with the entered details using builtin login module
login(request,user)
# Once logged in, redirect to home page
return HttpResponseRedirect(reverse('index'))
# if user account is not active, resturn a simple HttpResponse
else:
return HttpResponse('Your account is not active')
else:
# if the user is invalid or has entered invalid details
print("Someone tried to login with invalid details")
print("Username:{}, Password:{}".format(username,password))
return HttpResponse("Invalid Credentials, Please try again!")
else:
# if the user has not entered the login details
return render(request,'login.html',{})
Мой логин. html is
{% extends 'base.html' %}
{% block content %}
<div class="login">
<p class="logintext">Please Login</p>
<form class="form-group loginform" action="{% url 'user_login' %}" method="POST">
{% csrf_token %}
<div class="usr">
<label class="usr_label" for="username">Username</label>
<input class="usr_input" type="text" name="username" placeholder="Enter Username"><br/>
</div>
<div class="passwd">
<label class="passwd_label" for="password">Password</label>
<input class="passwrd_input" type="password" name="password" placeholder="Enter Password"<br/>
</div>
<div class="buttons">
<input class="btn btn-dark login-btn" type="submit" value="Login">
<a class="btn btn-dark register-btn" href="{% url 'registration' %}">New User?</a>
</div>
</form>
</div>
{% endblock %}
Любые ответы будут быть оцененным Заранее спасибо