5👍
✅
class PostForm(forms.Form):
message = forms.CharField(widget=forms.Textarea)
def set4x25(self):
self.fields['message'].widget.attrs = {'rows':'4', 'cols': '25'}
And in template:
{{ form.set4x25 }}
{{ form.message }}
You can customize this idea as you like.
2👍
You can do it in template with django-widget-tweaks:
{% load widget_tweaks %}
<p>{% render_field form.message rows="4" cols="25" %}</p>
or
{% load widget_tweaks %}
<p>{{ form.message|attr:"rows:4"|attr:"cols:25" }}</p>
- [Django]-Why does Django/Django REST Framework not validate CSRF tokens in-depth, even with enforce-CSRF?
- [Django]-Create Docker container with Django, npm and gulp
- [Django]-Django reference url in views.py
- [Django]-Architecture for syncing s3/cloudfront with database
- [Django]-Ajax: post array of integers to Django
0👍
You can’t do it from the template but you can:
-
Use CSS to specify the width and height of the textarea. This will override rows and cols attributes
-
Render the field manually:
{% with form.message as field %} <textarea name="{{ field.html_name }}" id="{{ field.html_initial_id }}" rows="4" cols="25">{% if field.data %}{{ field.data }}{% endif %}</textarea> {% endfor %}
- [Django]-How can you exclude an item in the query set while in a for loop?
- [Django]-File does not exist: /var/www/polls
- [Django]-Custom widget not validating only the first time
- [Django]-GeoDjango serialize GeoJSON skipping 'id' field
- [Django]-Render only one part of a MultiWidget in Django
Source:stackexchange.com