13

I've got a modelform that I would like to iterate through the form fields in the template but at the same time, when I encounter certain fields, to conditionally render extra html. Currently I am pulling the field name from field.html_name but I am not sure if that is the best way (it feels hackish somehow, like I should be using a getattr() filter or something...).

{% for field in form %}
<div class="form-group">

    {{ field }}

    {% if field.html_name == "location" %}
      <!-- CUSTOM HTML HERE -->
    {% endif %}

</div>   
{% endfor %}
3
  • What does the custom html do?
    – Sayse
    Aug 18, 2016 at 7:05
  • It adds another div. Mostly this is because the field with the name "location" is a ForeignKey and the div contains an <a href=> which allows to modify the location objects.
    – cowbert
    Aug 18, 2016 at 7:32
  • 1
    I can't find the exact part of the code at the minute but its something that django admin does similarly to add an add button for foreign key objects when using the admin model forms, so it may be worth looking into where they do that...
    – Sayse
    Aug 18, 2016 at 7:49

3 Answers 3

24

I have the same situation if I don't misunderstand your mean. My solution is field.name. Code sample:

{% if field.name == 'password' %}
   <input type="password" name="password" class="form-control" placeholder="{% trans 'Enter your password' %}">
{% else %}
   <input type="email" name="email" class="form-control" placeholder="{% trans 'Enter your email address' %}">
{% endif %}
3
  • 2
    @cowbert I guess this should be accepted answer since it actually answers the question?
    – Anupam
    May 19, 2017 at 12:22
  • I don't see how this is an improvement to the OP
    – cowbert
    Feb 28, 2018 at 18:25
  • Yes, I was looking for field.name while printing fields through a loop Jul 7, 2020 at 19:23
3

I don't know how you mean, but you can try this

{% for field in form %}
   {{ field }}
   {% if field.label == 'Location' %}
      <h1>Hi</h1>
   {% endif %}
{% endfor %}

Whereas, you would have set label in forms.py as

location = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'yourclass', 'placeholder': 'Enter your location',
               }), label=u'Location', max_length=100, required=True)
1
  • This doesn't help me, since it is exactly the same approach as setting/getting field.html_name
    – cowbert
    Aug 18, 2016 at 7:33
1

Have you considered using a widget or creating your own custom widget? https://docs.djangoproject.com/en/1.10/ref/forms/widgets/

E.g.: for adding just a css class or similiar to the existing input use the attrs argument

class MyForm(forms.Form):
     ...
     location = forms.CharField(
          ...,
          widget=Input(attrs={'class': 'location', 'style': 'background: red'}),
     )
     ...

Or creating a complete custom widget (take a look at how Input is implemented)

class LocationFieldWidget(Widget):
    def render(self, name, value, attrs=None):
        return mark_safe('custom html')

and then the form can be rendered in the template simply by

 {{ form }}
1
  • better yet, use django-widget-tweaks and keep the presentation information in the template only
    – Anupam
    May 19, 2017 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.