1. 程式人生 > >django 筆記6 Ajax

django 筆記6 Ajax

接收 checkbox 用戶 color borde javascrip CM 獲取 fir

感謝alex~
1.Django請求生命周期
    輸入url 進入 urls(路由系統) 指向 views(視圖函數)-》(獲取模板) 裏面的函數 再由函數返回字符串給用戶

2.路由系統
    /index/     ->  函數或類.as_view()
    /detail/(?P<nid>\d+)    函數(參數)或者類.as_view()(參數)
    /detail/      name="a1"      ->include(app01.urls->視圖中:reverse
                        
->模板中:{% url "a1" %} 3.視圖函數 FBV:函數 def index(request,*args,**kwargs): .. CBV:類 class Home(views.View): def get(self,request,*args, **kwargs) 獲取用戶請求中的數據: request.POST.get() #獲取name = request.GET.get() request.FILES.get() #checkbox,
select mutiple request.POST.getlist() request.GET.getlist() request.FILES.getlist() request.path_info 獲取當前請求的url 文件對象 = request.FILES.get() 文件對象.name 文件名 文件對象.size 文件大小字節 文件對象.chunks() 文件切片 需要for循環 f.write() #<form 特殊的設置></from
> 獲取用戶請求分會數據: render(request, "index.html" ,{obj:1234,k1:[1,2,3,4],k2:{name:"ljc"}}) #HTML模板的路徑 不是url redirect(url) return redirect(/cmdb/user_info/) HttpResponse(字符串) 4.模板語言 <html> <body> <h1>{{ obj }}<h1> 單值 <h1> {{ k1.3 }}<h1> <h1> {{k2.name}}<h1> {% for row in k1 %} #循環列表 <p>{{row}}<p> {%endfor%} {%for x,y in k2.items %} keys,values,items {{ x }} {{ y }} {%endfor%} <body> <html> 5.ORM a.創建類和字段 執行命令生成數據庫 class User(models.Model): nid = models.IntergerField() 數字 不需要加長度 name = models.CharField(max_length=64) 字符長度 只接收64個字符 python manage.py makemigrations python manage.py migrate # settings.py 註冊APP需要添加 b.操作數據庫 增刪改查都是可以增字典 **dic 增 models.User.object.create(nid=1,name="ljc") dic = {name:xx,nid:19} models.User.objects.create(**dic) obj = modeles.User.(nid=1,name="ljc") obj.save() 刪 models.User.objects.filter(id=1).delete() 改 models.User.objects.filter(id=2).update(nid=1,name="ljc") dic = {name:xx,nid:19} models.User.objects.filter(id__gt=1).update(**dic) 查 models.User.objects.filter(id=1,name="root")查 models.User.objects.filter(id__gt=1) models.User.objects.filter(id__lt=1) models.User.objects.filter(id__lte=1) dic = {name:xx,nid__gt:19} #nid大於19 models.User.objects.filter(**dic) 外鍵: class UserType(models.Model): caption = models.CharField(max_length=32) id caption #1.普通用戶 #2.VIP用戶 #3.遊客 class User(models.Model): age = models.IntergerFiled() name = models.CharField(max_length = 10) user_type = models.ForeignKey("UserType",to_field=id) #約束 但是生成數據時為user_type_id name age user_type_id ljc 18 3 zpt 18 2 cc 18 1 v1 = models.Business.objects.all() #QuerySet 對象 all() first() #[obj(id, caption, code),obj2(id,caption,code) ] {% for row in v1 %} <li>{{row.id}}-{{row.caption}}-{{row.code}}</li> {% endfor%} v2 = models.Business.objects.values(id,caption) #QuerySet 字典 #[{id:1,caption:"運維部"},{},{}] values變為字典了 {% for row in v2 %} <li>{{row.id}}-{{row.caption}}</li> {% endfor%} v3 = models.Business.objects.values_list(id, caption) #QuerySet 元組 #[(1,運維部),(2,開發)] {% for row in v3 %} <li>{{row.0}}-{{row.1}}</li> {% endfor%} models.Business.objects.get(id=1) 獲取到一個對象 但一般不用 **models.Business.objects.filter(id=1).first() 如果獲取對象! 如果存在為true 如果不存在返回null 外鍵 v = models.Host.objects.filter(nid__gt=0) v[0].b.caption ---> 通過點.來實現跨表查詢 __雙下劃線跨表 filter後面想跨表時都用雙下劃線 b__caption b__code v2 = models.Host.objects.filter(nid__gt=0).values(nid,hostname,b_id,b__caption) print(v2) 自循環 forloop.counter 從1開始 forloop.counter0 從0開始 forloop.revcounter 倒序排列 forloop.revcounter0 倒序0開始 forlopp.last {% for row in v1 %} <a>{{forloop.counter}}<a> {%endfor%} position:fixed absosulte relative .shade{ position:fixed; top:0; rigth:0; left:0; bottom:0; background:black; opacity:0.6; z-index:100; } .add-modal{ position:fixed; height:300px; width:400px; top:100px; left:50%; z-index:101; border:1px solied red; background:white; margin-left:-200px } .hide{ display:none; } <select> <option> <option> <select> redirect(/host)默認是以get render(request,host.html)以post Ajax提交 $.ajax({ url:/host, type:"POST 或者 GET", data:{k1:123, "k2":"root"}, success: function(data){ #這個函數等著服務端發送回復請求 //data是服務器端返回的字符串 var obj = JSON.parse(data) } } }) 內部都調用$.ajax 推薦還是$.ajax( {} ) $.get(url="",data={},) $.getJson $.post javascrip將字符串轉為字典 data = {1,2,3,4} var obj = JSON.parse(data); 將列表、字典轉為字符串 li = [1,2,3,4] JSON.stringify(li) 建議:永遠讓服務器端返回一個字典 return HttpResponse(json.dumps(字典)) python 字典/列表轉為字符串 json.dumps(字典) python 字符串轉為字典/列表 json.loads(字符串)

django 筆記6 Ajax