Django form入门详解--2
调整form的输出格式:
默认情况下form的格式化输出是基本table的样式的、但是django中还是为form提供发别的输出样式
1、默认的table样式输出
<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中的行的方式输出
2、把输出方式调整为p
<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>
3、以列表的方式输出
<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输出格式的总结:
1、{{ form }} 直接输出、这种默认的方式是以table的形式格式化的;
2、{{ form.as_p }} 以 p 段落的方式输出;
3、{{ form.as_ul }} 以 ul 列表的方式输出;
默认三种输出的不足:
如果这三种django自带的输出方式满足不了你的需求、那么“老铁扎心啦!”、你可能要自己定义form的输出了、
再这之前我们还要学习更多的关于form的东西才行
1、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标签。
2、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>
3、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>
4、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>
5、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>
----