27👍
✅
You can change the widget on the CharField to achieve the effect you are looking for.
search_input = forms.CharField(_(u'Search word'), required=False)
search_input.widget = forms.TextInput(attrs={'size': 10, 'title': 'Search',})
11👍
You can also try this:
search_input = forms.CharField(
_(u'Search word'),
required=False,
widget=forms.TextInput(attrs={'size': 10, 'title': 'Search',})
)
This is documented in the section entitled Styling widget instances.
👤Don
- [Django]-Celery (Redis) results backend not working
- [Django]-Django – is not a registered namespace
- [Django]-Django: guidelines for speeding up template rendering performance
8👍
There’s also the template-only solution using a filter. I recommend django-widget-tweaks
:
{% load widget_tweaks %}
{{ form.email|attr:'required:true' }}
- [Django]-How to send email via Django?
- [Django]-Is there a Django 1.7+ replacement for South's add_introspection_rules()?
- [Django]-'module' object has no attribute 'now' will trying to create a CSV
7👍
Old question, but for who-ever is looking for another alternative, there’s also this:
https://docs.djangoproject.com/en/1.6/topics/forms/modelforms/#overriding-the-default-fields
You can do something like this:
from django.forms import ModelForm, Textarea
from myapp.models import Author
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ('name', 'title', 'birth_date')
widgets = {
'name': Textarea(attrs={'cols': 80, 'rows': 20}),
}
- [Django]-How do I output HTML in a message in the new Django messages framework?
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-How to write django test meant to fail?
Source:stackexchange.com