rnevius
rnevius

Reputation: 27102

Splitting CreateView form into multiple pieces / Targeting individual fields

Is there a fast way to split a form, created by Django's CreateView cbv, into two or more parts? I'm trying to style two halves of a form differently via CSS. Similarly, can each of the fields/labels that are generated by the CreateView be individually targeted?

Upvotes: 1

Views: 351

Answers (1)

schillingt
schillingt

Reputation: 13731

Yes, you can handle the form in the template like any other form. See the docs here about customizing forms. Below is the code example with other-class added to two of the fields. You can customize the html however you need to.

<form action="/contact/" method="post">{% csrf_token %}
    {{ form.non_field_errors }}
    <div class="fieldWrapper">
        {{ form.subject.errors }}
        <label for="id_subject">Email subject:</label>
        {{ form.subject }}
    </div>
    <div class="fieldWrapper">
        {{ form.message.errors }}
        <label for="id_message">Your message:</label>
        {{ form.message }}
    </div>
    <div class="fieldWrapper other-class">
        {{ form.sender.errors }}
        <label for="id_sender">Your email address:</label>
        {{ form.sender }}
    </div>
    <div class="fieldWrapper other-class">
        {{ form.cc_myself.errors }}
        <label for="id_cc_myself">CC yourself?</label>
        {{ form.cc_myself }}
    </div>
    <p><input type="submit" value="Send message" /></p>
</form>

Upvotes: 1

Related Questions