from django.shortcuts import render, HttpResponse, redirect from app01 import models import json # Create your views here. def business(request): v1 = models.Business.objects.all() v2 = models.Business.objects.all().values('id', 'caption', 'code') v3 = models.Business.objects.all().values_list('id', 'caption', 'code') return render(request, 'business.html', {'v1': v1, 'v2': v2, 'v3': v3}) def host(request): if request.method == "GET": v1 = models.Host.objects.filter(nid__gt=0) v2 = models.Host.objects.filter(nid__gt=0).values('nid', 'hostname', 'b_id', 'b__caption') v3 = models.Host.objects.filter(nid__gt=0).values_list('nid', 'hostname', 'b_id', 'b__caption') b_list = models.Business.objects.all() return render(request, 'host.html', {'v1': v1, 'v2': v2, 'v3': v3, 'b_list': b_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, b_id=b ) return redirect('/host') def test_ajax(request): ret = {'status': True, 'error': None, 'data': None} try: h = request.POST.get('hostname') i = request.POST.get('ip') p = request.POST.get('port') b = request.POST.get('b_id') if h and len(h) > 5: models.Host.objects.create(hostname=h, ip=i, port=p, b_id=b) else: ret['status'] = False ret['error'] = "過短了" except Exception as e: ret['status'] = False ret['error'] = '請求錯誤' return HttpResponse(json.dumps(ret)) views.py
1 from django.db import models 2 3 # Create your models here. 4 5 class Business(models.Model): 6 caption = models.CharField(max_length=32) 7 code = models.CharField(max_length=32) 8 9 class Host(models.Model): 10 11 nid = models.AutoField(primary_key=True) 12 hostname = models.CharField(max_length=32,db_index=True) 13 ip = models.GenericIPAddressField(db_index=True) 14 port = models.IntegerField() 15 b = models.ForeignKey(to="Business",to_field='id') 16 17 models.py
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <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, .edit-modal, .delete-modal { position: fixed; height: 300px; width: 400px; top: 100px; left: 50%; z-index: 101; border: 1px solid red; 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 hid="{{ row.nid }}" bid="{{ row.b_id }}"> <td>{{ forloop.counter }}</td> <td>{{ row.hostname }}</td> <td>{{ row.ip }}</td> <td>{{ row.port }}</td> <td>{{ row.b.caption }}</td> <td> <a class="edit">編輯</a>|<a class="delete">刪除</a> </td> </tr> {% endfor %} </tbody> </table> <h1>主機列表(字典)</h1> <table border="1"> <thead> <tr> <th>主機名</th> <th>業務線名稱</th> </tr> </thead> <tbody> {% for row in v2 %} <tr hid="{{ row.nid }}" bid="{{ row.b_id }}"> <td>{{ row.hostname }}</td> <td>{{ row.b__caption }}</td> </tr> {% endfor %} </tbody> </table> <h1>主機列表(元組)</h1> <table border="1"> <thead> <tr> <th>主機名</th> <th>業務線名稱</th> </tr> </thead> <tbody> {% for row in v3 %} <tr hid="{{ row.0 }}" bid="{{ row.2 }}"> <td>{{ row.1 }}</td> <td>{{ row.3 }}</td> </tr> {% endfor %} </tbody> </table> <div class="shade hide"></div> <div class="add-modal hide"> <form id="add_form" method="POST" action="/host"> <div class="group"> <input id="host" type="text" placeholder="主機名" name="hostname"/> </div> <div class="group"> <input id="ip" type="text" placeholder="IP" name="ip"/> </div> <div class="group"> <input id="port" type="text" placeholder="端口" name="port"/> </div> <div class="group"> <select id="sel" name="b_id"> {% for op in b_list %} <option value="{{ op.id }}">{{ op.caption }}</option> {% endfor %} </select> </div> <input type="submit" value="提交"/> <a id="ajax_submit">悄悄提交</a> <input id="cancel" type="button" value="取消"/> <span id="erro_msg" style="color: red"></span> </form> </div> <div class="edit-modal hide"> <form id="edit_form" method="POST" action="/host"> <input type="text" name="nid" style="display:none"/> <input type="text" placeholder="主機名" name="hostname"/> <input type="text" placeholder="IP" name="ip"/> <input type="text" placeholder="端口" name="port"/> <select name="b_id"> {% for op in b_list %} <option value="{{ op.id }}">{{ op.caption }}</option> {% endfor %} </select> <a id="ajax_submit_edit">確認編輯</a> <input id="ajax_submit_cancel" type="button" value="取消"/> </form> </div> <div class="delete-modal hide"> <form id="delete_form" method="POST" action="/host"> <input type="text" name="nid" style="display:none"/> <input type="text" placeholder="主機名" name="hostname"/> <input type="text" placeholder="IP" name="ip"/> <input type="text" placeholder="端口" name="port"/> <select name="b_id"> {% for op in b_list %} <option value="{{ op.id }}">{{ op.caption }}</option> {% endfor %} </select> <a id="submit_delete">確認刪除</a> <input id="submit_cancle" 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'); }); $('#ajax_submit').click(function () { $.ajax({ url: "/test_ajax", type: 'POST', //data: {'hostname': $('#host').val(), 'ip': $('#ip').val(), 'port': $('#port').val(), 'b_id': $('#sel').val()}, data: $('#add_form').serialize(), success: function (data) { var obj = JSON.parse(data); if (obj.status) { location.reload(); } else { $('#erro_msg').text(obj.error); } } }) }); $('.edit').click(function () { $('.shade,.edit-modal').removeClass('hide'); var bid = $(this).parent().parent().attr('bid'); var nid = $(this).parent().parent().attr('hid'); $('#edit_form').find('select').val(bid); $('#edit_form').find('input[name="nid"]').val(nid); // 修改 /* $.ajax({ data: $('#edit_form').serialize() }); */ // modeletes.Host.objects.filter(nid=nid).update() }); $('#ajax_submit_cancel').click(function () { $('.shade,.edit-modal').addClass('hide'); }); $('.delete').click(function () { $('.shade,.delete-modal').removeClass('hide'); var bid = $(this).parent().parent().attr('bid'); var nid = $(this).parent().parent().attr('hid'); $('#delete_form').find('select').val(bid); $('#delete_form').find('input[name="nid"]').val(nid); }); $('#submit_cancle').click(function () { $('.shade,.delete-modal').addClass('hide'); }); }) </script> </body> </html> host.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 }}- {{ row.code }}</li> {% endfor %} </ul> <h1>業務線列表(元組)</h1> <ul> {% for row in v3 %} <li>{{ row.0 }} - {{ row.1 }}- {{ row.2 }}</li> {% endfor %} </ul> </body> </html> business.html
1 """s14day20 URL Configuration 2 3 The `urlpatterns` list routes URLs to views. For more information please see: 4 https://docs.djangoproject.com/en/1.10/topics/http/urls/ 5 Examples: 6 Function views 7 1. Add an import: from my_app import views 8 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 Class-based views 10 1. Add an import: from other_app.views import Home 11 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 Including another URLconf 13 1. Import the include() function: from django.conf.urls import url, include 14 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 15 """ 16 from django.conf.urls import url 17 from django.contrib import admin 18 from app01 import views 19 urlpatterns = [ 20 url(r'^admin/', admin.site.urls), 21 url(r'^business$', views.business), 22 url(r'^host$', views.host), 23 url(r'^test_ajax$', views.test_ajax), 24 url(r'^app$', views.app), 25 url(r'^ajax_add_app$', views.ajax_add_app), 26 # url(r'^business_add', views.business), 27 ] 28 29 url.py
八、外鍵:html
1 """s14day20 URL Configuration 2 3 The `urlpatterns` list routes URLs to views. For more information please see: 4 https://docs.djangoproject.com/en/1.10/topics/http/urls/ 5 Examples: 6 Function views 7 1. Add an import: from my_app import views 8 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 Class-based views 10 1. Add an import: from other_app.views import Home 11 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 Including another URLconf 13 1. Import the include() function: from django.conf.urls import url, include 14 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 15 """ 16 from django.conf.urls import url 17 from django.contrib import admin 18 from app01 import views 19 urlpatterns = [ 20 url(r'^admin/', admin.site.urls), 21 url(r'^business$', views.business), 22 url(r'^host$', views.host), 23 url(r'^test_ajax$', views.test_ajax), 24 url(r'^app$', views.app), 25 url(r'^ajax_add_app$', views.ajax_add_app), 26 # url(r'^business_add', views.business), 27 ] 28 29 url.py
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <style> 7 .host-tag{ 8 display: inline-block; 9 padding: 3px; 10 border: 1px solid red; 11 background-color: palevioletred; 12 } 13 .hide{ 14 display: none; 15 } 16 .shade{ 17 position: fixed; 18 top: 0; 19 right: 0; 20 left: 0; 21 bottom: 0; 22 background: black; 23 opacity: 0.6; 24 z-index: 100; 25 } 26 .add-modal,.edit-modal{ 27 position: fixed; 28 height: 300px; 29 width: 400px; 30 top:100px; 31 left: 50%; 32 z-index: 101; 33 border: 1px solid red; 34 background: white; 35 margin-left: -200px; 36 } 37 </style> 38 </head> 39 <body> 40 41 <h1>應用列表</h1> 42 <div> 43 <input id="add_app" type="button" value="添加" /> 44 </div> 45 <table border="1"> 46 <thead> 47 <tr> 48 <td>應用名稱</td> 49 <td>應用主機列表</td> 50 </tr> 51 </thead> 52 <tbody> 53 {% for app in app_list %} 54 <tr aid="{{ app.id }}"> 55 <td>{{ app.name }}</td> 56 <td> 57 {% for host in app.r.all %} 58 <span class="host-tag" hid="{{ host.nid }}"> {{ host.hostname }} </span> 59 {% endfor %} 60 </td> 61 <td> 62 <a class="edit">編輯</a> 63 </td> 64 </tr> 65 {% endfor %} 66 </tbody> 67 </table> 68 69 70 71 <div class="shade hide"></div> 72 <div class="add-modal hide"> 73 <form id="add_form" method="POST" action="/app"> 74 <div class="group"> 75 <input id="app_name" type="text" placeholder="應用名稱" name="app_name" /> 76 </div> 77 <div class="group"> 78 <select id="host_list" name="host_list" multiple> 79 {% for op in host_list %} 80 <option value="{{ op.nid }}">{{ op.hostname }}</option> 81 {% endfor %} 82 </select> 83 </div> 84 85 <input type="submit" value="提交" /> 86 <input id="add_submit_ajax" type="button" value="Ajax提交" /> 87 </form> 88 89 90 </div> 91 92 <div class="edit-modal hide"> 93 <form id="edit_form" method="POST" action="/host"> 94 <input type="text" name="nid" style="display:none" /> 95 <input type="text" placeholder="應用名稱" name="app" /> 96 <select name="host_list" multiple> 97 {% for op in host_list %} 98 <option value="{{ op.nid }}">{{ op.hostname }}</option> 99 {% endfor %} 100 </select> 101 <a id="ajax_submit_edit" >確認編輯</a> 102 </form> 103 104 105 </div> 106 107 <script src="/static/jquery-1.12.4.js"></script> 108 <script> 109 $(function(){ 110 111 $('#add_app').click(function(){ 112 $('.shade,.add-modal').removeClass('hide'); 113 }); 114 115 $('#cancel').click(function(){ 116 $('.shade,.add-modal').addClass('hide'); 117 }); 118 119 120 $('#add_submit_ajax').click(function(){ 121 $.ajax({ 122 url: '/ajax_add_app', 123 // data: {'user': 123,'host_list': [1,2,3,4]}, 124 data: $('#add_form').serialize(), 125 type: "POST", 126 dataType: 'JSON', // 內部 127 traditional: true, 128 success: function(obj){ 129 console.log(obj); 130 }, 131 error: function () { 132 133 } 134 135 }) 136 }); 137 138 139 $('.edit').click(function(){ 140 141 $('.edit-modal,.shade').removeClass('hide'); 142 143 var hid_list = []; 144 $(this).parent().prev().children().each(function(){ 145 var hid = $(this).attr('hid'); 146 hid_list.push(hid) 147 }); 148 149 $('#edit_form').find('select').val(hid_list); 150 // 若是發送到後臺 151 // 152 /* 153 obj = models.Application.objects.get(id=ai) 154 obj.name = "新Name" 155 obj.save() 156 obj.r.set([1,2,3,4]) 157 */ 158 159 160 }) 161 162 }) 163 164 165 166 </script> 167 </body> 168 </html> 169 170 app.html
1 from django.shortcuts import render,HttpResponse,redirect 2 from app01 import models 3 import json 4 # Create your views here. 5 6 def business(request): 7 v1 = models.Business.objects.all() 8 # QuerySet 9 # [obj(id,caption,code),obj(id,caption,code),obj(id,caption,code) ] 10 11 v2 = models.Business.objects.all().values('id','caption') 12 # QuerySet 13 # [{'id':1,'caption': '��ά��'},{'id':1,'caption': '��ά��'},...] 14 15 v3 = models.Business.objects.all().values_list('id','caption') 16 # QuerySet 17 # [(1����ά��),(2,����)] 18 return render(request, 'business.html', {'v1': v1,'v2': v2, 'v3': v3}) 19 20 # def host(request): 21 # v1 = models.Host.objects.filter(nid__gt=0) 22 # # QuerySet [hostobj(ip.host,����һ������(..)),] 23 # # for row in v1: 24 # # print(row.nid,row.hostname,row.ip,row.port,row.b_id,row.b.caption,row.b.code,row.b.id,sep='\t') 25 # # print(row.b.fk.name) 26 # # return HttpResponse("Host") 27 # v2 = models.Host.objects.filter(nid__gt=0).values('nid','hostname','b_id','b__caption') 28 # # QuerySet: [ {} ] 29 # # print(v2) 30 # # for row in v2: 31 # # print(row['nid'],row['hostname'],row['b_id'],row['b__caption']) 32 # 33 # v3 = models.Host.objects.filter(nid__gt=0).values_list('nid','hostname','b_id','b__caption') 34 # # QuerySet: [ {} ] 35 # # print(v2) 36 # return render(request, 'host.html', {'v1': v1,'v2': v2,'v3': v3}) 37 38 def host(request): 39 if request.method == "GET": 40 v1 = models.Host.objects.filter(nid__gt=0) 41 v2 = models.Host.objects.filter(nid__gt=0).values('nid','hostname','b_id','b__caption') 42 v3 = models.Host.objects.filter(nid__gt=0).values_list('nid','hostname','b_id','b__caption') 43 44 b_list = models.Business.objects.all() 45 46 return render(request, 'host.html', {'v1': v1,'v2': v2,'v3': v3,'b_list':b_list}) 47 48 elif request.method == "POST": 49 50 h = request.POST.get('hostname') 51 i = request.POST.get('ip') 52 p = request.POST.get('port') 53 b = request.POST.get('b_id') 54 # models.Host.objects.create(hostname=h, 55 # ip=i, 56 # port=p, 57 # b=models.Business.objects.get(id=b) 58 # ) 59 models.Host.objects.create(hostname=h, 60 ip=i, 61 port=p, 62 b_id=b 63 ) 64 return redirect('/host') 65 66 67 def test_ajax(request): 68 69 ret = {'status': True, 'error': None, 'data': None} 70 try: 71 h = request.POST.get('hostname') 72 i = request.POST.get('ip') 73 p = request.POST.get('port') 74 b = request.POST.get('b_id') 75 if h and len(h) > 5: 76 models.Host.objects.create(hostname=h, 77 ip=i, 78 port=p, 79 b_id=b) 80 else: 81 ret['status'] = False 82 ret['error'] = "過短了" 83 except Exception as e: 84 ret['status'] = False 85 ret['error'] = '請求錯誤' 86 return HttpResponse(json.dumps(ret)) 87 88 89 def app(request): 90 if request.method == "GET": 91 app_list = models.Application.objects.all() 92 # for row in app_list: 93 # print(row.name,row.r.all()) 94 95 host_list = models.Host.objects.all() 96 return render(request,'app.html',{"app_list": app_list,'host_list': host_list}) 97 elif request.method == "POST": 98 app_name = request.POST.get('app_name') 99 host_list = request.POST.getlist('host_list') 100 print(app_name,host_list) 101 102 obj = models.Application.objects.create(name=app_name) 103 obj.r.add(*host_list) 104 105 return redirect('/app') 106 107 108 def ajax_add_app(request): 109 ret = {'status':True, 'error':None, 'data': None} 110 111 app_name = request.POST.get('app_name') 112 host_list = request.POST.getlist('host_list') 113 obj = models.Application.objects.create(name=app_name) 114 obj.r.add(*host_list) 115 return HttpResponse(json.dumps(ret)) 116 117 views.py