I am creating a web application using Django and in login i am getting non in authentication can you please help
Models.py
class CustomUser(AbstractBaseUser):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
name = models.CharField(max_length=255)
address = models.CharField(max_length=255)
country = models.CharField(max_length=255)
qualifications = models.TextField(blank=True)
skills = models.TextField(blank=True)
exprence = models.IntegerField(max_length=255)
exp_details = models.CharField(max_length=255)
role = models.CharField(max_length=50, default='user') # Add the 'role' field
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
forms.py
class SignUpForm(UserCreationForm):
qualifications = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter qualifications as comma-separated values'}), required=False)
skills = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter skills as comma-separated values'}), required=False)
class Meta:
model = CustomUser
fields = ['email', 'name', 'address', 'country', 'qualifications', 'skills', 'exprence', 'exp_details']
def clean_qualifications(self):
qualifications = self.cleaned_data.get('qualifications')
return qualifications.split(',') if qualifications else []
def clean_skills(self):
skills = self.cleaned_data.get('skills')
return skills.split(',') if skills else []
def save(self, commit=True):
user = super().save(commit=False)
user.qualifications = self.cleaned_data["qualifications"]
user.skills = self.cleaned_data["skills"]
user.set_password(self.cleaned_data["password1"]) # Hash the password using make_password
if commit:
user.save()
return user
class LoginForm(forms.Form):
email = forms.EmailField(label='Email')
password = forms.CharField(label='Password', widget=forms.PasswordInput)
views.py
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
return redirect('login') # Redirect to login page after successful signup
else:
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
def custom_login(request):
error_message = None
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
# Check if the email exists in the database
user = authenticate(username=email, password=password)
if user is not None:
# Check the password using check_password
if check_password(password, user.password):
# Passwords match, login the user
login(request, user)
messages.success(request, 'Login successful.')
return redirect('home') # Redirect to the home page after login
else:
# Passwords do not match
error_message = 'Invalid email or password.'
else:
# User not found or authentication failed
error_message = 'Invalid email or password.'
else:
# Form validation failed
error_message = 'Form is not valid.'
else:
form = LoginForm()
return render(request, 'login.html', {'form': form, 'error_message': error_message})