Django-forloop added and increased number of data many examples

I. Introduction

  We often see, if you need to add in front of each row of data numbers we supposed to? Is inside the database id, wrong, we need forloop use a for loop inside. We also need to add data to many, it is a foreign key to a table inside the insert data. Today we'll write a data example of many of the increase

Two, forloop add numbers

2.1, single cycle

Description: We are under a single for loop to get the serial number.

① sequence starting from 1, i.e.: forloop.counter

{% for row in v2 %}
    <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
        <td>{{ forloop.counter }}</td> #顺序从1开始
        <td>{{ row.hostname }}</td>
        <td>{{ row.business__caption }}</td>
    </tr>
{% endfor %}

Figure:

② order starting from 0, namely: forloop.counter0

{% for row in v2 %}
    <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
        <td>{{ forloop.counter0 }}</td>  #顺序从0开始
        <td>{{ row.hostname }}</td>
        <td>{{ row.business__caption }}</td>
    </tr>
{% endfor %}

Figure:

③ In a flashback ends, i.e.: forloop.revcounter

{% for row in v2 %}
    <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
        <td>{{ forloop.revcounter }}</td>
        <td>{{ row.hostname }}</td>
        <td>{{ row.business__caption }}</td>
    </tr>
{% endfor %}

Figure:

⑤ flashback ends with 0, namely: forloop.revcounter0

{% for row in v2 %}
    <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
        <td>{{ forloop.revcounter0 }}</td>  #倒叙以0结束
        <td>{{ row.hostname }}</td>
        <td>{{ row.business__caption }}</td>
    </tr>
{% endfor %}

Figure:

 2.2, for statement nested loops

Description: We need to get information nested loop, then you need to use the function of the forloop.parentloop

{% for i in v2 %}
    {% for row in v2 %}
        <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
            <td>{{ forloop.parentloop }}</td>  #获取父循环的信息
            <td>{{ row.hostname }}</td>
            <td>{{ row.business__caption }}</td>
        </tr>
    {% endfor %}
{% endfor %}

Figure:

If you want to get the value of counter field of the parent cycle:

{% for i in v2 %}
    {% for row in v2 %}
        <tr h-id="{{ row.nid }}" b-id="{{ row.business_id }}">
            <td>{{ forloop.parentloop.counter }}</td>  #父循环信息中获取counter值
            <td>{{ row.hostname }}</td>
            <td>{{ row.business__caption }}</td>
        </tr>
    {% endfor %}
{% endfor %}

 如图:

其他的以此类推,不过这玩意几乎用不到,这边只是介绍一下。

三、增加增加一对多数据示例

3.1、urls.py的连接

from django.contrib import admin
from django.urls import path,re_path
from app01 import views
 
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^host/$',views.host)
]

3.2、templates的模板信息host.html

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .hide{
            display: none;
        }
        .shade{
           position: fixed;
            top:0;
            right:0;
            left:0;
            bottom: 0;
            background: black;
            opacity: 0.6;
            z-index: 100;
        }
        .add-modal{
            position: fixed;
            height: 300px;
            width: 400px;
            top:200px;
            left: 50%;
            z-index: 101;
            border: 1px solid white;
            background: white;
            margin-left: -200px;
        }
    </style>
</head>
<body>
    <h1>主机列表(对象)</h1>
    <div>
        <input id="add_host" type="button" value="添加"/>
    </div>
    <table border="1">
        <thead>
            <tr>
                <th>序号</th>
                <th>主机名</th>
                <th>IP</th>
                <th>端口</th>
                <th>业务线名称</th>
                <th>业务线编码</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v1 %}
                <tr h-id="{{ row.nid }}" b-id="{{ row.business.id }}">
                    <td>{{ forloop.counter }}</td>
                    <td>{{ row.hostname }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.business.caption }}</td>
                    <td>{{ row.business.code }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <div class="shade hide"></div>
    <div class="add-modal hide">
        <form method="post" action="/host/">
            <div class="group">
                <input type="text" placeholder="主机名" name="hostname">
            </div>
            <div class="group">
                <input type="text" placeholder="IP" name="ip">
            </div>
            <div class="group">
                <input type="text" placeholder="端口" name="port">
            </div>
            <div class="group">
                <select name="b_id">
                    {% for row in business_list %}
                        <option value="{{ row.id }}">{{ row.caption }}</option>
                    {% endfor %}
                </select>
            </div>
            <input type="submit" value="提交">
            <input id="cancel" type="button" value="取消">
        </form>
    </div>
    <script src="/static/jquery-1.12.4.js"></script>
    <script>
        $(function(){
            $("#add_host").click(function(){
                $(".shade,.add-modal").removeClass("hide");
            });

            $("#cancel").click(function(){
                $(".shade,.add-modal").addClass("hide");
            });
        })
    </script>
</body>

host.html

3.3、view.py的代码

def host(request):
    if request.method == "GET":
        v1 = models.Host.objects.filter(nid__gte=1)
        business_list = models.Business.objects.all()
 
        return render(request,"host.html",{'v1':v1,"business_list":business_list})
    elif request.method == "POST":
        h = request.POST.get("hostname")
        i = request.POST.get("ip")
        p = request.POST.get("port")
        b = request.POST.get("b_id")
        models.Host.objects.create(
            hostname=h,
            ip=i,
            port=p,
            business_id=b
        )
        return redirect("/host/")  #注意了,这边不要用render,因为render是需要渲染数据的,如果用这个,你压根就没有往里面传数据,跳转页面就会变成空,所以还是增加完毕直接跳转即可

如图:

模态对话框:

 

Guess you like

Origin www.cnblogs.com/FangYi0328/p/11775285.html