Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Wendi Adistya
Wendi Adistya
3,671 Points

help me, how to insert css class in django form

how to insert css class on django form. in my case i want to insert bootstrap class on my form property likes input tag, label and between them.

bootstrap without django form :

<form role="form">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="exampleInputEmail1">Email address</label>
                      <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                    </div>
                    <div class="form-group">
                      <label for="exampleInputPassword1">Password</label>
                      <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                    </div>
</form>

i want to add <div class="form-group"> and class class="form-control" on each input tag to :

     <form method="post" action="">
        {% csrf_token %}
        {{ form.as_p }}
        <input type='submit' value='Create Post' />
      </form>

how?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

If you would like to add custom HTML around each fform field, you will need to loop over the fields and add the HTML directly. The docs have examples on looping over the form's fields.

{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }} {{ field }}
        {% if field.help_text %}
        <p class="help">{{ field.help_text|safe }}</p>
        {% endif %}
    </div>
{% endfor %}
Wendi Adistya
Wendi Adistya
3,671 Points

thank you chris, i'll try