0

I am trying to add a class attribute to some input elements on my Django form but I'm not sure how to do it specifically. Currently I just have a placeholder attribute, and I'd like to start adding classes/id's so that I can style the form. I have two questions. The first one is how would I go about adding a class/id attribute to the existing placeholder attribute(s) in the following code:

forms.py

class EditUserProfileForm(forms.ModelForm):
    class Meta:
        model = models.UserProfile
        fields = '__all__'

        widgets = {
            'description': forms.TextInput(attrs={'placeholder': 'Motto'}),
            'city': forms.TextInput(attrs={'placeholder': 'City'}),
            'website': forms.TextInput(attrs={'placeholder': 'Website/Brand'}),
            'phone': forms.TextInput(attrs={'placeholder': 'Phone Number'}),
            }

and secondly, after I do this would I need to then makemigrations/migrate? From my understanding you need to migrate whenever a database level change has been made, but I'm not sure what that means entirely.

Any insight is always greatly appreciated. Thanks

1
  • You are not making a database level change when modifying a form definition, so no migrations are required. Commented Oct 20, 2018 at 4:47

3 Answers 3

1

First, to add a class to your widgets simply declare it the same way as you did it for placeholder attr. For example...

widgets = {
            'description': forms.TextInput(attrs={'class': 'my_class', 'placeholder': 'Motto'},)
           }

Second, you should perform makemigrations/migrate whenever you change your models. In order to adjust your database tables to the new models. Read Django docs for the details.

Sign up to request clarification or add additional context in comments.

1 Comment

I wonder if it is possible to add classes in dict notation. I tried attrs=dict(class='my_class') but this obviously did not work, since Django (i.e. Python) was expecting a class declaration after class
1

You can use Widget Tweaks. With help of this you can add classes to your input and edit other attribute also. Here is good tutorial.

Comments

0

Below I have changed your form. For your second question Do you need to call makemigration? Answer is no. You only need to call makemigration and migrate, when you make any changes in your models.py, for views.py or forms.py etc. You do not need.

class EditUserProfileForm(forms.ModelForm):

    class Meta:
        model = models.UserProfile
        fields = '__all__'

    widgets = {
        'description': forms.TextInput(attrs={'class':'#add class name','placeholder': 'Motto'}),
        'city': forms.TextInput(attrs={'class':'#add class name','placeholder': 'City'}),
        'website': forms.TextInput(attrs={'class':'#add class name','placeholder': 'Website/Brand'}),
        'phone': forms.TextInput(attrs={'class':'#add class name','placeholder': 'Phone Number'}),
        }

Note: You also don't need to add class name, unless you have specific requirement. If you want to add class name for css, then you can simply write id_description , id_city, You can check your id in browser developers tool.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.