I am using django forms and I want to use Twitter Bootstrap's css in my html. so my template looks like this:
{% for field in form %} <div class="form-group"> {{ field.label_tag }}<!--Same thing as : <label for="{{field.id_for_label}}"></label> --> <input type="{{field.type}}" class="form-control" id="{{field.auto_id}}" placeholder="Email"> </div> {% endfor %}
I can't figure out out to get the type value. {{field.type}}
.
Is there any way to get the type of the input field in django templates?
Thanks in advance
Update: The reason I am asking this question is because I want to be able to apply bootstrap classes to the input element. But in Bootstrap3, to use the default css for input types you would have to add form-control class to the input element like so: <input type="text" class="form-control" id="{{field.auto_id}}" placeholder="">.
If I use django's field {{field}}
then I can't add the form-control class. I hope this clarifies some things.
I also saw this app https://github.com/dyve/django-bootstrap3 that looks like it does what I wanted to do. It surprises me that django doesn't allow accessing the form type to allow more flexibility.
Basically to extract data from a form field of a form, all you have to do is use the form. is_valid() function along with the form. cleaned_data. get() function, passing in the name of the form field into this function as a parameter.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
ModelChoiceField , which is a ChoiceField whose choices are a model QuerySet .
{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.
According to https://stackoverflow.com/a/31538555/254553
You can use:
{{ [FIELD].field.widget.input_type }}
[FIELD]
is your field name.
I don't think you need to worry about the field_type
. Django will itself handle that for you depending on the form field.
Lets say we have a ContactForm
like:
class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(widget=forms.Textarea) sender = forms.EmailField() cc_myself = forms.BooleanField(required=False)
Then {{form.subject}}
will automatically create <input>
element in the template.
<input id="id_subject" type="text" name="subject" maxlength="100" />
Similarly, {{form.message}}
in the template will create:
<input type="text" name="message" id="id_message" />
Though if you really need to get the form field type in the template, you can create a custom template filter like below.
from django import template register = template.Library() @register.filter(name='field_type') def field_type(field): return field.field.widget.__class__.__name__
Now, in your template, you need to do something like:
{{form.field_name|field_type}}
In the above example, {{form.message|field_type}}
will return TextInput
.
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