Eeyore
Eeyore

Reputation: 2126

Custom Django Form + Inline Error Messages

I'd like to know how can use Django's automatic form generation...

<form action="/contact/" method="POST">
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>

To achieve the following output (note the custom field in the middle of the form and error class within the wrapping div).

<form action="/contact/" method="POST">

    <div class="input error">
        <label for="id_subject">E-mail subject:</label>
        <span>Error Message</span>
    </div>

    <div class="input error">
        <label for="id_message">Your message:</label>
        <span>Error Message</span>
    </div>

    <!-- CUSTOM CONTAINER -->
    <div class="custom-container">
        <h2>Custom Content</h2>
    </div>

    <div class="input error">
        <label for="id_sender">Your email address:</label>
        <span>Error Message</span>
    </div>

    <div class="input error">
        <label for="id_cc_myself">CC yourself?</label>
        <span>Error Message</span>
    </div>

    <div><input type="submit" value="Send message" /></div>
</form>

Upvotes: 1

Views: 2377

Answers (2)

Skylar Saveland
Skylar Saveland

Reputation: 11464

You can checkout django-uni-form, http://github.com/pydanny/django-uni-form it will get you a little closer to what you are trying to do with the wrapping divs.

I agree with SmileyChris that you will be hard pressed to find a generic convenience method that allows you do so much.

But, between django-uni-form and judicious use of widgets you can get close.

Upvotes: 4

SmileyChris
SmileyChris

Reputation: 10794

The Form.as_* methods are pretty much just scaffolding. As soon as you want to do anything past just listing the fields in a consistent manor, you shouldn't be using them any more.

Using some of the logic in the documentation about reusable form templates you should be able to achieve what you want.

Upvotes: 7

Related Questions