django获取表单数据
django获取单表数据的三种方式
v1 = models.Business.objects.all()
# v1是QuerySet的列表 ,内部元素都是对象
v2 = models.Business.objects.all().values('id','caption') //这里不加all()也可以
# v2是QuerySet的列表 ,内部元素都是字典
v3 = models.Business.objects.all().values_list('id','caption') //这里不加all()也可以
# v3是QuerySet的列表 ,内部元素都是元组
def business(request):
v1 = models.Business.objects.all()
v2 = models.Business.objects.all().values('id','caption')
v3 = models.Business.objects.all().values_list('id','caption')
print(v1,v2,v3)
return render(request, 'business.html', {'v1': v1, 'v2': v2, 'v3': v3})
business.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>业务线列表(对象)</h1>
<ul>
{% for row in v1 %}
<li>{{ row.id }} - {{ row.caption }} - {{ row.code }}</li>
{% endfor %}
</ul>
<h1>业务线列表(字典)</h1>
<ul>
{% for row in v2 %}
<li>{{ row.id }} - {{ row.caption }}</li>
{% endfor %}
</ul>
<h1>业务线列表(元组)</h1>
<ul>
{% for row in v3 %}
<li>{{ row.0 }} - {{ row.1 }}</li>
{% endfor %}
</ul>
</body>
</html>
获取一个对象
方法一:如果存在就返回一个对象,如果不存在就报错,不推荐
models.Business.objects.get(id=1)
方法二:如果存在就返回一个对象,不存在就返回none,推荐
models.Business.objects.filter(id=1).first()
外键:
class Business(models.Model):
caption = models.CharField(max_length=32)
code = models.CharField(max_length=32,null=True,default='SA')
class Host(models.Model):
id = models.AutoField(primary_key=True)
hostname = models.CharField(max_length=32)
ip = models.GenericIPAddressField(protocol='ipv4')
port = models.IntegerField()
b = models.ForeignKey(to='Business',to_field='id')
v = models.Host.objects.filter(nid__gt=0)
v[0].b.caption ----> 通过点(.)进行跨表

实例:跨表查询的三种方式
def host(request):
host_obj = models.Host.objects.all()
host_obj2 = models.Host.objects.filter(b__caption='运维').values('id','hostname','ip','port','b__code')
for i in host_obj2:
print(i['id'],i['hostname'],i['ip'],i['port'],i['b__code'])
host_obj3 = models.Host.objects.filter(b__caption='运维').values_list('id', 'hostname', 'ip', 'port', 'b__code')
return render(request,'host.html',{'host_obj':host_obj,'host_obj2':host_obj2,'host_obj3':host_obj3})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>对象</h2>
<table border="1px">
<thead>
<tr>
<th>ID</th>
<th>主机名</th>
<th>ip</th>
<th>port</th>
<th>业务线</th>
<th>业务线编码</th>
</tr>
</thead>
<tbody>
{% for i in host_obj %}
<tr hid="{{ i.id }}" bid="{{ i.b_id }}">
<td>{{ forloop.counter }}</td>
<td>{{ i.hostname }}</td>
<td>{{ i.ip }}</td>
<td>{{ i.port }}</td>
<td>{{ i.b.caption }}</td>
<td>{{ i.b.code }}</td>
</tr>
{% endfor %} </tbody>
</table>
<h2>字典</h2>
<table border="1px">
<thead>
<tr>
<th>ID</th>
<th>主机名</th>
<th>ip</th>
<th>port</th>
<th>业务线编码</th>
</tr>
</thead>
<tbody> {% for i in host_obj2 %}
<tr hid="{{ i.id }}" bid="{{ i.b_id }}">
<td>{{ forloop.counter }}</td>
<td>{{ i.hostname }}</td>
<td>{{ i.ip }}</td>
<td>{{ i.port }}</td> <td>{{ i.b__code }}</td>
</tr>
{% endfor %} </tbody>
</table>
<h2>元组</h2>
<table border="1px">
<thead>
<tr>
<th>ID</th>
<th>主机名</th>
<th>ip</th>
<th>port</th>
<th>业务线编码</th>
</tr>
</thead>
<tbody> {% for i in host_obj3 %}
<tr hid="{{ i.0 }}" bid="{{ i.b_id }}">
<td>{{ forloop.counter }}</td>
<td>{{ i.1 }}</td>
<td>{{ i.2 }}</td>
<td>{{ i.3 }}</td>
<td>{{ i.4 }}</td>
</tr>
{% endfor %} </tbody>
</table> </body>
</html>
django获取表单数据的更多相关文章
- Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据
用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...
- ASP.NET MVC 获取表单数据
public class Person { public string Name{get;set;} public string Phone{get;set;} } view层 @model Mode ...
- 1.3(学习笔记)Servlet获取表单数据
一.Servlet获取表单数据 表单提交数据经由Servlet处理,返回一个处理结果显示在页面上, 那么如何获取表单提交的参数进出相应的处理呢? 主要用到以下方法: String getParame ...
- JSP简单练习-获取表单数据
在JSP中,server端程序与client交互最经常使用的方法就是採用表单提交数据.表单提交的方法主要有两种,一种是get方法.还有一种是post方法.两者最大的差别:使用get方法提交的数据会显示 ...
- php学习笔记-获取表单数据
在网页上经常要填写用户名和密码,点击确认按纽之后,用户名和密码经过前端处理之后发送到了服务器上,那么服务器端怎么获取到这些用户提交的数据呢?就是通过超级全局变量 _POST和_GET 先拿_POST做 ...
- Servlet 响应 响应相关与重定向 请求 获取表单数据2种方法
一.HttpServletResponse (响应) 包括下面三个: 1.响应消息行 HTTP/1.1 200 OK 200是HTTP状态码, 代表请求已成功. (查httpservletres ...
- JS--轻松设置获取表单数据
接触过Angularjs的都知道,ng支持双向绑定,我们可以轻轻松松的通过ngModel将我们的值绑定到界面,当修改了值提交表单的时候不需要再重新通过ID去重新抓取输入框信息了.那对于我们开发前台网站 ...
- FromData获取表单数据
一般想要不刷新页面提交数据时,可以使用ajax提交.如果数据量不大可以自己写json数据用ajax提交到后台服务,但是数据量多且需要动态添加数据时,自己写json格式数据就有点麻烦了,这时候就需要Fo ...
- 用Servlet获取表单数据
用Servlet获取表单数据 在webroot下新建userRegist2.jsp 代码如下: <%@ page contentType="text/html;charset=gb23 ...
随机推荐
- hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)
#include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...
- 2019寒假算法基础集训营1 - B 小a与"204"
题目: 小a非常喜欢这个数字,因为. 现在他有一个长度为的序列,其中只含有这三种数字 设为序列中第个数,你需要重新排列这个数列,使得最大(公式的含义是:每个数与前一个数差的平方的和) 注意:我们默认 ...
- 2018.4.3 配置AD服务器步骤
net ads命令表 配置AD服务器步骤:1. 安装rpm依赖包yum -y install pam_krb5* krb5-libs* krb5-workstation* krb5-devel* kr ...
- python笔记-1(import导入、time/datetime/random/os/sys模块)
python笔记-6(import导入.time/datetime/random/os/sys模块) 一.了解模块导入的基本知识 此部分此处不展开细说import导入,仅写几个点目前的认知即可.其 ...
- Blender 精确建模3D打印注意事项
首先参照前面的<Blender的单位:一图弄懂Blender的单位>设置好自己环境的长度单位. 下面的注意事项,没有先后关系,遇到的就会补充. 1. 模型需要进行布尔计算前,在物件我是下, ...
- 实验吧—Web——WP之 what a fuck!这是什么鬼东西?
打开链接——> 发现是一大堆符号,可能有些人见过这些样子的,这是一种编码方式,叫:jother编码 jother编码是一种运用于javasscript语言中利用少量字符构造精简的匿名函数方法,对 ...
- EnableAutoConfiguration注解的工作原理(org.springframework.boot.autoconfigure.EnableAutoConfiguration=core.bean.MyConfig)
EnableAutoConfiguration注解的工作原理(org.springframework.boot.autoconfigure.EnableAutoConfiguration=core.b ...
- ionic1实现热更新以版本检测更新安装包的方法
1.需要下载热更新插件:插件名称是cordova-hot-code-push 首先打开cli,执行命令 npm install -g cordova-hot-code-push-cli 此功能主要是为 ...
- LeetCode - Rotate String
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- DevExpress皮肤样式
[时间] 2016-02-15 11:41:11 天气晴 没有雾霾难得的好天气!!! [工具] (1)Visual Studio 2015 (2)DevExpress15.2.3 [感言] 一直以来都 ...