I have this in terminal
'invalid_login': 'Please enter a correct %(username)s and password. Note that both fields may be case-sensitive.', 'inactive': 'This account is inactive.'}
[
but, I have saved my username and password via browser, setting very common username and password that is impossible to write wrong
may problem lay in form that is used during registration, or it happens because of registration form, or it happens because of models?
My models
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=255, blank=True)
last_name = models.CharField(max_length=255, blank=True)
bio = models.CharField(max_length=255, blank=True)
image = models.ImageField(blank=True)
Forms:
from django import forms
from django.contrib.auth.models import User
from users.models import Profile
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
class ProfileRegistrationForm(UserCreationForm):
username = forms.CharField(required=True, max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Enter username'}))
email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'placeholder': 'Enter email'}))
password1 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Enter password'}), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Confirm password'}), label="Password")
class Meta:
model = User
fields = ['first_name', 'last_name','username', 'email', 'password1', 'password2']
def save(self, commit=True):
user = super().save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
profile = Profile(user = user, first_name = self.cleaned_data['first_name'],
last_name = self.cleaned_data['last_name'])
if commit:
profile.save()
return profile
def clean_username(self):
username = self.cleaned_data['username'].lower()
new = User.objects.filter(username=username)
if new.count():
raise forms.ValidationError("User already exists")
return username
def clean_email(self):
email = self.cleaned_data['email'].lower()
new = User.objects.filter(email=email)
if new.count():
raise forms.ValidationError("Email already used")
return email
def clean_password2(self):
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords doesnt match")
return password2
class ProfileLoginForm(AuthenticationForm):
username = forms.CharField(max_length=255, required=True)
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'password']
and view:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .forms import ProfileLoginForm, ProfileRegistrationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import AuthenticationForm
def login_user(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
user = form.get_user()
login(request, user)
return redirect('profile')
else:
print(form.error_messages)
else:
form = ProfileLoginForm()
return render(request, 'login.html', {'form': form})
def register(request):
if request.method == 'POST':
form = ProfileRegistrationForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Registration success')
return redirect('login')
else:
form = ProfileRegistrationForm
return render(request, 'register.html', {'form': form})
@login_required
def profile(request):
render(request, 'profile.html', {'profile': profile})
Firstly I have tried to use custom login form, but then I started using base AuthenticationForm, but still problem happenes
After analyzing your files there are few issues that might be causing errors
In your login view function :
def login_user(request):
if request.method == 'POST':
form = AuthenticationForm(request, data=request.POST) # Pass the request object
In the same view function substitute error_messages
and add only errors
else:
print(form.errors) # Show actual form errors
In you register view function's else block add parenthesis
form = ProfileRegistrationForm # wrong
form = ProfileRegistrationForm() # right