{{ theform.address }}
{{ theform.phone }}
This is what I do in my templates.
However, what if I want to add placeholder="Username"
to the input text field? (Custom attribute)
<input type="text" name="address" id="id_address" placeholder="username"/>
In order to add a class or id attribute to a form in Django, we have to place a widget=form. TextInput (or widget= form. EmailInput, if email input) within the form field (because it's a text field). Inside of this widget, we then have to put, attrs={'class':'some_class'}.
ChoiceField in Django Forms is a string field, for selecting a particular choice out of a list of available choices. It is used to implement State, Countries etc. like fields for which information is already defined and user has to choose one. It is used for taking text inputs from the user.
attrs . A dictionary containing HTML attributes to be set on the rendered DateInput and TimeInput widgets, respectively. If these attributes aren't set, Widget. attrs is used instead.
Add the attrs
keyword argument to your field constructor's widget
, and include write your attribute in there:
address = forms.TextField(widget=forms.TextInput(attrs={'placeholder': 'username'}))
If you want to see it in action, take a look at django-registration's forms.py.
Alternatively, you can use the http://pypi.python.org/pypi/django-widget-tweaks app:
{% load widget_tweaks %}
...
{{ theform.address|attr:"placeholder:username" }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With