感谢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(字符串)
原文:https://www.cnblogs.com/Liang-jc/p/9191799.html