views:

669

answers:

2

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>
+6  A: 

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.

SmileyChris
I looked at the reusable form templates - but in this case it's not about reusability but more about customization.I am fine with hard coded form as long as I can get the error classes working.
Eeyore
You're example code shows that you want a custom container in the middle, so again, you can't rely on the `as_*` methods, as you *do* want customization. To clarify, there is currently no way to output HTML a single field using the logic contained within these "scaffolding" methods.
SmileyChris
+1  A: 

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.

skyl
django-uni-form has a layout module which handles this exactly problem just nicely.
pydanny