Django user registration form not showing properly

Tags: , , , ,



I am making a Django project where there are two types of users customer and restaurant. Both can register separately. Everything is running fine except that I cannot add CSS class to password field and there are warning showing around password field which I don’t how they are coming.

Models.py

class User(AbstractUser):
is_customer = models.BooleanField(default=False)
is_restaurant = models.BooleanField(default=False)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)


class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
food_pref = models.CharField(max_length=10, default='veg')


class Restaurant(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)

Forms.py soryy for bad code pasting.Just for clarification class ‘CustomerSignupform’ is parent class.

class CustomerSignUpForm(UserCreationForm):
     first_name = forms.CharField(required=True)
     last_name = forms.CharField(required=True)
     food_pref = forms.CharField(required=True)

class Meta(UserCreationForm.Meta):
    model = User

@transaction.atomic
def save(self):
    user = super().save(commit=False)
    user.is_customer = True
    user.first_name = self.cleaned_data.get('first_name')
    user.last_name = self.cleaned_data.get('last_name')
    user.food_pref = self.cleaned_data.get('food_pref')
    user.save()
    customer = Customer.objects.create(user=user)
    customer.food_pref = self.cleaned_data.get('food_pref')
    customer.save()
    return user

def __init__(self, *args, **kwargs):
    super(CustomerSignUpForm, self).__init__(*args, **kwargs)

    self.fields['username'].widget.attrs.update({
        'class': 'form-control',
        "name": "username"})
    self.fields['first_name'].widget.attrs.update({
        'class': 'form-control',
        "name": "username"})
    self.fields['last_name'].widget.attrs.update({
        'class': 'form-control',
        "name": "username"})
    self.fields['food_pref'].widget.attrs.update({
        'class': 'form-control',
        "name": "food_pref"})

Views.py

class customer_register(CreateView):
    model = User
    form_class = CustomerSignUpForm
    template_name = 'login/customer_register.html'

def form_valid(self, form):
    user = form.save()
    login(self.request, user)
    return redirect('/')

Register User View

So I have got similar problem for restaurant registration. Thank You

Answer

Could you try this. I think somehow it will help you.

            self.fields['password1'].widget.attrs['class'] = 'form-group'
            self.fields['password2'].widget.attrs['class'] = 'form-group'

            self.fields['password1'].widget.attrs['placeholder'] = 'Password'
            self.fields['password2'].widget.attrs['placeholder'] = 'Confirm Passoword'

            self.fields['password1'].label = ''
            self.fields['password2'].label = ''

            self.fields['password1'].help_text = ''
            self.fields['password2'].help_text = ''


Source: stackoverflow