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,520 points
7,369 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,840 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
5,240 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,840 points
2,775 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,840 points
2,451 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,840 points
1,565 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,840 points
4,630 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,840 points
3,176 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