Django基础--3

Posted aline2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django基础--3相关的知识,希望对你有一定的参考价值。

ORM

django一对多的操作

获取表单的三种方式:views

获取对象            .all()      html中获取方式为 row.id,row,host

获取字典            .all().values("字段","字段","字段",……)  row.id,row,host

获取元祖            .all().values_list("字段","字段","字段",……) row.0,row.1

技术分享图片
#views

from django.shortcuts import render
from app import models
# Create your views here.
def home(request):
    v1 = models.Host.objects.all()
    print(v1)
    #<QuerySet [<Host: Host object (1)>, <Host: Host object (2)>, <Host: Host object (3)>]> 对象
    v2 = models.Host.objects.all().values("host","port")
    print(v2)
    #< QuerySet[{‘host‘: ‘a‘, ‘port‘: 80}, {‘host‘: ‘b‘, ‘port‘: 81}, {‘host‘: ‘c‘, ‘port‘: 82}] >  字典类型
    v3 = models.Host.objects.all().values_list("host","port")
    print(v3)
    #<QuerySet [(‘a‘, 80), (‘b‘, 81), (‘c‘, 82)]>  元祖类型
    retur

#html中

<body>
    <h1>主机信息</h1>
    <ul>
        {% for row in v1 %}
            <li>{{ row.id }}--- {{ row.host }}--- {{ row.ip }}--- {{ row.port }}--- {{ row.dept_id }}</li>
        {% endfor %}
    </ul>

    <h1>主机信息</h1>
    <ul>
        {% for row in v2 %}
            <li>{{ row.host }}--- {{ row.port }}</li>
        {% endfor %}
    </ul>

    <h1>主机信息</h1>
    <ul>
        {% for row in v3 %}
            <li>{{ row.0 }}--- {{ row.1 }}</li>
        {% endfor %}
    </ul>
</body>
View Code

一对多的跨表操作

获取对象 filter(id__gte=1)

 

技术分享图片
#views

def host(request):
    v1 = models.Host.objects.filter(id__gte=1)
    return render(request,"host.html",{"v1":v1})


#urls.py


from app import views
path(host/, views.host),




#host.html
<body>
    <table border="1">
        <thead>
            <tr>
                <th>id</th>
                <th>主机名</th>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>业务线</th>
                <th>部门</th>
                <th>部门代码</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v1 %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.host }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept_id }}</td>
                    <td>{{ row.dept.dept }}</td>
                    <td>{{ row.dept.dept_en }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
View Code

 

以上将所有的数据都显示出来了,然而实际中并用不到这么多,这里可以隐藏两个表的id,因每次都会循环tr,直接在<tr>中定义两个属性放id

这里修改下tbody里面内容即可

<tbody>
            {% for row in v1 %}
                <tr hid = "{{ row.id }}" bid = "{{ row.dept.id }}">
                    <td>{{ row.host }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept.dept }}</td>
                </tr>
            {% endfor %}
        </tbody>

获取字典 filter(id__gte=1).values("id","ip", "port", "dept_id","dept__dept")

 

#views.py

#获取类型为字典
    v2 = models.Host.objects.filter(id__gte=1).values("id","ip", "port", "dept_id","dept__dept")
    #print(v2)
    # <QuerySet [{‘ip‘: ‘1.1.1.1‘, ‘host‘: ‘a‘, ‘dept__dept‘: ‘运维‘}, {‘ip‘: ‘1.1.1.2‘, ‘host‘: ‘b‘, ‘dept__dept‘: ‘销售‘}, {‘ip‘: ‘1.1.1.3‘, ‘host‘: ‘c‘, ‘dept__dept‘: ‘信息部‘}]>
    for i in v2:
        print(i["id"],i["ip"],i["port"],i["dept__dept"])
  return render(request,"host.html",{"v2":v2})

 

前端操作:

<h2>业务线-字典</h2>
    <table border="1">
        <thead>
            <tr>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>部门</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v2 %}
                <tr hid = "{{ row.id }}" bid = "{{ row.dept_id }}">
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept__dept }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

获取元祖  filter(id__gte=1).values_list("id","ip", "port", "dept_id","dept__dept")

 

v3 =  models.Host.objects.filter(id__gte=1).values_list("id","ip", "port", "dept_id","dept__dept")
    for j in v3:
        print(j[0],j[1],j[2],j[3],j[4],)
    return render(request,"host.html",{"v3":v3})

 

<h2>业务线-元祖</h2>
    <table border="1">
        <thead>
            <tr>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>部门</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v3 %}
                <tr hid = "{{ row.0 }}" bid = "{{ row.3 }}">
                    <td>{{ row.1 }}</td>
                    <td>{{ row.2 }}</td>
                    <td>{{ row.4}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

 


以上是关于Django基础--3的主要内容,如果未能解决你的问题,请参考以下文章

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

JSP基础

[vscode]--HTML代码片段(基础版,reactvuejquery)

Django REST框架--认证和权限

如何在 Django 中显式重置模板片段缓存?

使用 Django 模板作为片段