调整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>

    

  

----

Django form入门详解--2的更多相关文章

  1. Django form入门详解--1

     form在django中的作用: 1.可以用于自动生成form的html 2.数据校验 3.与model一在一起使用.可以大的方便数据驱动型网站的开发 编程中有许多的东西是“不可描述”的.只有动手去 ...

  2. django session入门详解

    概括性的讲: 1.django默认是打开对session的支持的 2.默认情况下session相关的数据会保存在数据库中.浏览器端只保存了session id session 的科普: 1.动态网站中 ...

  3. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  4. SQL注入攻防入门详解

    =============安全性篇目录============== 本文转载 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机 ...

  5. SQL注入攻防入门详解(2)

    SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱 ...

  6. Quartz 入门详解

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  7. Redis快速入门详解

    Redis入门详解 Redis简介 Redis安装 Redis配置 Redis数据类型 Redis功能 持久化 主从复制 事务支持 发布订阅 管道 虚拟内存 Redis性能 Redis部署 Redis ...

  8. [转]SQL注入攻防入门详解

    原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============ ...

  9. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

随机推荐

  1. Mybatis 自动生成代码,数据库postgresql

    最近做了一个项目,使用Mybatis自动生成代码,下面做一下总结,被以后参考: 一.提前准备: 1.工具类:mybatis-generator-core-1.3.2.jar 2.postgresql驱 ...

  2. C#判断网站是否能访问或者宕机的方法

    最近有位朋友说他有很多网址,可能有些已经过期或者不能访问了.自己去一个一个点可以,但又很麻烦! 再过一段时间又要去检查一次,每次都这样就不方便了! 于是就做了个小程序给帮他检测一下. 以下做了一个例子 ...

  3. SQL Server中order by的使用,我们来填坑

    看似很简单是不是? 单列排序,没有任何问题 select * from tableA where age>1 order by age /*后面可以跟上ASC.DESC,默认是ASC升序排列*/ ...

  4. 2721: [Violet 5]樱花|约数个数

    先跪一发题目背景QAQ 显然x,y>n!,然后能够设y=n!+d 原式子能够化简成 x=n!2d+n! 那么解的个数也就是n!的因子个数,然后线性筛随便搞一搞 #include<cstdi ...

  5. 纯正商业级小程序开发(完结版).txt

        链接: https://pan.baidu.com/s/1LzlDslKxSUy3UV9o1aDKhg 提取码: sq7e 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  6. 〖Android〗CyanogenMod同步错误的解决

    1. 错误信息: repo sync CyanogenMod/Superuser Fetching project CyanogenMod/Superuser Fetching projects: % ...

  7. Android开发之将拍摄的图片传至服务器

    package com.example.oldtab; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...

  8. 微软Xbox One无线手柄控制机器人

    ROS中的joy包提供了游戏手柄的驱动,并且包含joy_node节点,这一节点可以发布包含手柄按钮和轴信息的Joy消息.在终端中输入下面命令,安装joy包: $ sudo apt-get instal ...

  9. codevs 2010 求后序遍历

    时间限制: 1 s空间限制: 64000 KB题目描述 Description输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列.输入描述 Input Description共两行,第一行一个字符 ...

  10. 微信小程序开发学习资料

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...