144👍
TextField
is a type of field, which when used by a ModelForm
by default uses the Textarea
widget. Fields deal with backend storage, widgets with front-end editing.
So you need to specify the widget you want to go with your field. You want to create a ModelForm
and specify the widget
you want to use, per the documentation:
from django import forms
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
widgets = {
'summary': forms.Textarea(attrs={'rows':4, 'cols':15}),
}
26👍
Here is the form specific equivalent for changing the rows/cols attribute to a textarea. Use the forms.Textarea widget and set attrs dictionary values:
class LocationForm(forms.ModelForm):
auto_email_list = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 2, 'cols': 40}))
notes = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 4, 'cols': 40}))
class Meta:
model = Location
exclude = ['id']
Hope this helps those looking for form specific
- [Django]-Why does django's prefetch_related() only work with all() and not filter()?
- [Django]-Django model with 2 foreign keys from the same table
- [Django]-How to send html email with django with dynamic content in it?
16👍
If you’re using django-crispy-forms, in your __init__()
function you can access the widget attrs like so:
self.fields['summary'].widget.attrs['rows'] = 4
self.fields['summary'].widget.attrs['columns'] = 15
I suppose you could set the attrs this way it in a non crispy-form ModelForm, but you may as well do it in your widget definition, as shown above. I just found this post and thought I’d share how I did it with crispyforms.
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-Pycharm error Django is not importable in this environment
- [Django]-Django: Where to put helper functions?
5👍
You can also use jQuery as follows:
$(document).ready(function(){
$("textarea").attr({"rows": "4",
"cols": "15"
});
});
- [Django]-Can I call a view from within another view?
- [Django]-Uninstall Django completely
- [Django]-Django 1.4 timezone.now() vs datetime.datetime.now()
3👍
First you have to install django widget tweaks by using:
pip install django-widget-tweaks
Then you have to go to setting.py and add ‘widget_tweaks’ to installed apps like this:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'widget_tweaks',
)
Then go to your html template and replace this with {{ field }}, to resize your text field:
{% load widget_tweaks %}
{{ mytextareafield|attr:"rows:4" | "cols:30" }}
- [Django]-Django REST Framework – Separate permissions per methods
- [Django]-Remove leading and trailing slash / in python
- [Django]-Why is Django throwing error "DisallowedHost at /"?
2👍
The django-widget-tweaks
package makes this quite simple. In your template:
{% load widget_tweaks %}
...
{{ mytextareafield|attr:"rows:3" }}
- [Django]-Django: Redirect to previous page after login
- [Django]-Setting default value for Foreign Key attribute in Django
- [Django]-How do I create a login API using Django Rest Framework?
2👍
Use Widget in Meta.
Keep in Mind that you must add that field in modelform also
class MyModelForm(forms.ModelForm):
address = forms.CharField()
class Meta:
model = MyModel
exclude = ()
widgets = {
address': forms.Textarea(attrs={'rows':1, 'cols':15}),
}
- [Django]-Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'
- [Django]-Referencing multiple submit buttons in django
- [Django]-How can I use Bootstrap with Django?
-1👍
The first answer may be the right one for the question, but the django-widget-tweaks package is also a great option for changing more attributes, for instance set specific CSS class.
So consider that option if you want to alter another attributes easily.
- [Django]-Django: Get an object form the DB, or 'None' if nothing matches
- [Django]-How can I programmatically obtain the max_length of a Django model field?
- [Django]-Do I need Nginx with Gunicorn if I am not serving any static content?