自定义Django表单+内联错误消息

Eey*_*ore 1 forms django

我想知道如何使用Django的自动表单生成...

<form action="/contact/" method="POST">
    {{ form.as_p }}
    <input type="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

要实现以下输出(请注意包装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>
Run Code Online (Sandbox Code Playgroud)

Smi*_*ris 7

这些Form.as_*方法几乎只是脚手架.一旦你想要做任何事情,只是在一致的庄园中列出字段,你就不应该再使用它们了.

使用文档中关于可重用表单模板的一些逻辑,您应该能够实现您想要的.