How to change the width of form elements created with ModelForm in Django

0 votes

Here is my product class:

class ProductForm(ModelForm):
    long_desc = forms.CharField(widget=forms.Textarea)
    short_desc = forms.CharField(widget=forms.Textarea)
    class Meta:
        model = Product

And the template code...

{% for f in form %}
    {{ f.name }}:{{ f }}
{% endfor %}

f is the actual form element.

How can I change the width of a textarea form element if I used ModelForm to create it?

Jul 29, 2020 in Python by kartik
• 37,510 points
6,859 views

1 answer to this question.

0 votes

Hello @kartik,

The easiest way for your use case is to use CSS. It's a language meant for defining presentation. The code generated by form, take note of the ids for fields that interest you, and change appearance of these fields through CSS.

Example for long_desc field in your ProductForm (when your form does not have a custom prefix):

#id_long_desc {
    width: 300px;
    height: 200px;
}

Hope it helps!!

Thank You!!

answered Jul 29, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

How to count the number of elements in a list?

To count the number of elements of ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
4,593 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to clear a database from the CLI with manage.py in Django?

Hello @kartik, To drop the database and run syncdb again. ...READ MORE

answered Aug 7, 2020 in Python by Niroj
• 82,880 points
2,421 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,041 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,189 views
0 votes
1 answer

How to change the file name of an uploaded file in Django?

Hello @kartik, The basic way is import os os.rename('a.txt', 'b.html') For ...READ MORE

answered Jul 3, 2020 in Python by Niroj
• 82,880 points
4,151 views
0 votes
1 answer

How to extend the User model with custom fields in Django?

Hello @kartik, Define an object manager for your ...READ MORE

answered Aug 3, 2020 in Python by Niroj
• 82,880 points
2,797 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP