2014-10-04 58 views
0

這裏被包裹輸入字段是我的註冊表單當W3C Django的驗證在<tr><th>

<form id="user_form" method="post" action="/accounts/register/"> 
    {% csrf_token %} 
    {{ user_form }} 
    <br> 
    <input type="submit" class="btn btn-default pull-right" value="Register"> 
    <br> 
</form> 

模板代碼當我檢查我的直播網站的HTML或我的本地環境中我看到下面的HTML

<form id="user_form" method="post" action="/accounts/register/"> 
    <input type="hidden" name="csrfmiddlewaretoken" value="blah"> 
    <label for="id_username">Username:</label><input class="form-control" id="id_username" name="username" type="text"> 
    <label for="id_email">Email:</label><input class="form-control" id="id_email" name="email" type="email"> 
    <label for="id_password">Password:</label><input class="form-control" id="id_password" name="password" type="password"> 
    <br> 
    <input type="submit" class="btn btn-default pull-right" value="Register"> 
    <br> 
</form> 

但是,當我確認在W3C我收到錯誤,因爲我的形式

Stray start tag tr. <tr><th><label for="id_username">Username:</label></th><td><inp… 

而且我看到的標記已經改變

<form id="user_form" method="post" action="/accounts/register/"> 
    <input type='hidden' name='csrfmiddlewaretoken' value='blah' /> 
    <tr><th><label for="id_username">Username:</label></th><td><input class="form-control" id="id_username" name="username" type="text" /></td></tr> 
    <tr><th><label for="id_email">Email:</label></th><td><input class="form-control" id="id_email" name="email" type="email" /></td></tr> 
    <tr><th><label for="id_password">Password:</label></th><td><input class="form-control" id="id_password" name="password" type="password" /></td></tr> 
    <br> 
    <input type="submit" class="btn btn-default pull-right" value="Register"> 
    <br> 
</form> 

回答

0

你只需要使用:

{{ user_form.as_p }} 

這將渲染<p>標籤內形式的標籤和字段。該docs給你關於輸出幾個選項,也可以手動通過遍歷領域呈現形式,具體如下:

<!-- first output any hidden fields first --> 
    <div style="display: none;"> 
     <!-- I've wrapped this in a div for W3C validation --> 
     {{ user_form.hidden_fields }} 
    </div> 

{% for field in user_form.visible_fields %} 
    <div class="some-class"> 
     {{ field.label_tag }} 
     {{ field }} 
    </div><!--/.some-class--> 
{% endfor %} 

我建議你給上述一試,只是用手工繪製的實驗。

+0

as_p很感謝!仍然困惑,爲什麼W3C的抓取工具看到了額外的標記,但我沒有,我在多個瀏覽器中測試。 – beansontoast 2014-10-04 15:33:49