調整form的輸出格式:html
默認狀況下form的格式化輸出是基本table的樣式的、可是django中仍是爲form提供發別的輸出樣式django
一、默認的table樣式輸出app
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {% csrf_token %} {{ form }} <!-- 這裏採用的是django的默認輸出方式 --> <input type="submit" value="submit"> </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> <input type='hidden' name='csrfmiddlewaretoken' value='gR8rgaARcg8F3GWX4igz8xE6EXTrdyr8QoVJpZVrG9Cc3ibgvz9FioIfPNTRUh59' /> <tr><th><label for="id_your_name">Your name:</label></th><td><input type="text" name="your_name" maxlength="100" required id="id_your_name" /></td></tr> <input type="submit" value="submit"> </form> </body> </html>
能夠看到默認狀況下輸出內容以table中的行的方式輸出學習
二、把輸出方式調整爲pui
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {% csrf_token %} <!-- 用 p 的方式格式化輸入 --> {{ form.as_p }} <input type="submit" value="submit"> </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> <input type='hidden' name='csrfmiddlewaretoken' value='rDlHnoLxu991YKhlb22qVuYbSDDfCHwX1a8Zwd67Y2DyYmwECjVw5l2k3tDFjqaY' /> <!-- 用 p 的方式格式化輸入 --> <p><label for="id_your_name">Your name:</label> <input type="text" name="your_name" maxlength="100" required id="id_your_name" /></p> <input type="submit" value="submit"> </form> </body> </html>
三、以列表的方式輸出url
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {% csrf_token %} <!-- 用 p 的方式格式化輸入 --> {{ form.as_ul }} <input type="submit" value="submit"> </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> <input type='hidden' name='csrfmiddlewaretoken' value='ZgjUKC9RLivh2aZKdHB3c2QOn4ZhwcMVzN6cTrurfbZO2Me3EYu9mTUXyUZHdVqW' /> <!-- 用 p 的方式格式化輸入 --> <li><label for="id_your_name">Your name:</label> <input type="text" name="your_name" maxlength="100" required id="id_your_name" /></li> <input type="submit" value="submit"> </form> </body> </html>
對form輸出格式的總結:spa
一、{{ form }} 直接輸出、這種默認的方式是以table的形式格式化的;code
二、{{ form.as_p }} 以 p 段落的方式輸出;orm
三、{{ form.as_ul }} 以 ul 列表的方式輸出;csrf
默認三種輸出的不足:
若是這三種django自帶的輸出方式知足不了你的需求、那麼「老鐵扎心啦!」、你可能要本身定義form的輸出了、
再這以前咱們還要學習更多的關於form的東西才行
一、form.field_name:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {{ form.your_name }} </form> </body> </html>
能夠看到模板中直接對form.field_name 進行打印、輸出以下:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> <input type="text" name="your_name" maxlength="100" required id="id_your_name" /> </form> </body> </html>
扎心啦、一個字段就直接對應一個 input標籤。
二、form.field_name.lable:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {{ form.your_name.label}} </form> </body> </html>
生成的代碼以下
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> Your name </form> </body> </html>
三、form.field_name.label_tag:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {{ form.your_name.label_tag}} </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> <label for="id_your_name">Your name:</label> </form> </body> </html>
四、form.field_name.id_for_label:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {{ form.your_name.id_for_label }} </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> id_your_name </form> </body> </html>
五、form.field_name.value:
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action={% url 'your-name' %} method="POST"> {{ form.your_name.value }} </form> </body> </html>
<html> <head> <title>your name</title> <meta charset="utf8"/> </head> <body> <form action=/app01/yourname method="POST"> None </form> </body> </html>
----