from django import forms from django.contrib.auth.forms import AuthenticationForm from .models import User class UserRegistrationForm(forms.ModelForm): password = forms.CharField( widget=forms.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter your password', }) ) confirm_password = forms.CharField( widget=forms.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': 'Confirm your password', }) ) class Meta: model = User fields = ['first_name', 'last_name', 'email', 'password'] widgets = { 'first_name': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter your first name', }), 'last_name': forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter your last name', }), 'email': forms.EmailInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter your email address', }), } def clean(self): cleaned_data = super().clean() password = cleaned_data.get('password') confirm_password = cleaned_data.get('confirm_password') if password and confirm_password and password != confirm_password: raise forms.ValidationError("Passwords do not match.") class UserLoginForm(AuthenticationForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(UserLoginForm, self).__init__(*args, **kwargs) # Change the username field label and placeholder to 'Email' self.fields['username'].label = 'Email' self.fields['username'].widget.attrs.update({'placeholder': 'Enter your email'}) self.fields['password'].widget.attrs.update({'placeholder': 'Enter your password'})