day20-django forloop添加序號和增加一對多數據示例


一、前言

  我們經常見到的,如果需要在每一行的數據前面加上序號咋辦?是用數據庫里面的id,錯了,我們需要用一個for循環里面的東西forloop。還有我們需要添加一對多的數據,就是往一個有外鍵的表里面插入數據。今天我們就來寫一個增加一對多的數據示例

二、forloop添加序號

2.1、單循環

說明:我們在單個for循環下,獲取序號。

①順序從1開始,即:forloop.counter

1
2
3
4
5
6
7
{ % 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 % }

如圖:

②順序從0開始,即:forloop.counter0

1
2
3
4
5
6
7
{% 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 %}

如圖:

③倒敘以1結束,即:forloop.revcounter

1
2
3
4
5
6
7
{% 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 %}

如圖:

⑤倒敘以0結束,即:forloop.revcounter0

1
2
3
4
5
6
7
{% 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 %}

如圖:

 2.2、for語句嵌套循環

說明:我們需要獲取嵌套循環的信息的話,那么就需要用到forloop.parentloop的功能了

1
2
3
4
5
6
7
8
9
{% 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 %}

 如圖:

如果想獲取父循環中的counter字段的值:

1
2
3
4
5
6
7
8
9
{% 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的連接

1
2
3
4
5
6
7
8
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

 1 <head>
 2     <meta charset="UTF-8">
 3     <title>Title</title>
 4     <style>
 5         .hide{
 6             display: none;
 7         }
 8         .shade{
 9            position: fixed;
10             top:0;
11             right:0;
12             left:0;
13             bottom: 0;
14             background: black;
15             opacity: 0.6;
16             z-index: 100;
17         }
18         .add-modal{
19             position: fixed;
20             height: 300px;
21             width: 400px;
22             top:200px;
23             left: 50%;
24             z-index: 101;
25             border: 1px solid white;
26             background: white;
27             margin-left: -200px;
28         }
29     </style>
30 </head>
31 <body>
32     <h1>主機列表(對象)</h1>
33     <div>
34         <input id="add_host" type="button" value="添加"/>
35     </div>
36     <table border="1">
37         <thead>
38             <tr>
39                 <th>序號</th>
40                 <th>主機名</th>
41                 <th>IP</th>
42                 <th>端口</th>
43                 <th>業務線名稱</th>
44                 <th>業務線編碼</th>
45             </tr>
46         </thead>
47         <tbody>
48             {% for row in v1 %}
49                 <tr h-id="{{ row.nid }}" b-id="{{ row.business.id }}">
50                     <td>{{ forloop.counter }}</td>
51                     <td>{{ row.hostname }}</td>
52                     <td>{{ row.ip }}</td>
53                     <td>{{ row.port }}</td>
54                     <td>{{ row.business.caption }}</td>
55                     <td>{{ row.business.code }}</td>
56                 </tr>
57             {% endfor %}
58         </tbody>
59     </table>
60 
61     <div class="shade hide"></div>
62     <div class="add-modal hide">
63         <form method="post" action="/host/">
64             <div class="group">
65                 <input type="text" placeholder="主機名" name="hostname">
66             </div>
67             <div class="group">
68                 <input type="text" placeholder="IP" name="ip">
69             </div>
70             <div class="group">
71                 <input type="text" placeholder="端口" name="port">
72             </div>
73             <div class="group">
74                 <select name="b_id">
75                     {% for row in business_list %}
76                         <option value="{{ row.id }}">{{ row.caption }}</option>
77                     {% endfor %}
78                 </select>
79             </div>
80             <input type="submit" value="提交">
81             <input id="cancel" type="button" value="取消">
82         </form>
83     </div>
84     <script src="/static/jquery-1.12.4.js"></script>
85     <script>
86         $(function(){
87             $("#add_host").click(function(){
88                 $(".shade,.add-modal").removeClass("hide");
89             });
90 
91             $("#cancel").click(function(){
92                 $(".shade,.add-modal").addClass("hide");
93             });
94         })
95     </script>
96 </body>
97 
98 host.html

 

3.3、view.py的代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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是需要渲染數據的,如果用這個,你壓根就沒有往里面傳數據,跳轉頁面就會變成空,所以還是增加完畢直接跳轉即可

如圖:

模態對話框:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM