class BookForm(forms.Form): title=forms.CharField() price=forms.FloatField() def add(): if request.method=="POST": form=BookForm(request.POST) if form.is_valid(): pass else: pass #form=BookForm() return render(request,"add.html") add.html: # 方案1 <form action=""> <p>名称<input type="text"name="title"></p> <p>价格<input type="text"name="price"></p> <input type="submit"> </form> # 方案2 <form action="" method="post"> {% csrf_token %} {% for field in form %} <div> {{ field.label }} {{ field }} </div> {% endfor %} <input type="submit"> </form> ###################################################### forms.ChoiceField(Field) ----select forms.ModelChoiceField(ChoiceField) ----select forms.ModelMultipleChoiceField(ModelChoiceField) ----select multiple 跨域 jsonp 同源是指,协议,域名,端口相同。 cors admin配置 会议室预定
POST: 浏览器-------------------->server "请求首行 Content-Type:url_encode a=1&b=2" "请求首行 Content-Type:application/json '{"a":1,"b":2}'" 在django的wsgi的request中: request.body:元数据'{"a":1,"b":2}' if 请求头中的Content-Type==url_encode: request.POST=解码a=1&b=2 Q: 方式1: q=Q() q.connection="or" q.children.append("pk",1) q.children.append("user_id",1) q.children.append("room_id",1) Book.objects.filter(q) 方式2: Book.objects.filter(Q(pk=1)|Q(user_id=1)|Q(room_id=1))
1 admin的使用 list_display list_display_links search_fields list_filter action class BookConfig(admin.ModelAdmin): list_display list_display_links search_fields list_filter action admin.site.register(Book,BookConfig) 2 知识点 url()的使用 情况1:url(r'^book/', views.book), # book(request) 情况2 分发: url(r"^yuan/", ([ url(r'^test01/', ([ url(r'^test04/', test04), url(r'^test05/', test05), ], None, None)), url(r'^test02/', test02), url(r'^test03/', test03), ], None, None) ) 单例模式 生成单例模式的方式: (1)使用 __new__ (2)使用模块 class A() pass a=A() admin源码: 1 启动文件 class StarkConfig(AppConfig): name = 'stark' def ready(self): autodiscover_modules('stark') 2 注册 admin.py admin.site.register(Book,BookConfig) 源码: class AdminSite(): def __init__(self, name='admin'): self._registry = {} def register(self,model,admin_class): if not admin_class: admin_class = ModelAdmin self._registry[model] = admin_class(model, self) site=AdminSite() 3 设计url 如何通过model类变量获取该模型的字符串名称和该模型所在app的字符串名称: print("===>", model._meta.model_name) print("===>", model._meta.app_label) 在ModelStark中: self.model: 用户当前访问的模型表 查看页面: 表头 表数据 search action 分页 filter 增删改(modelForm)
Queryset: 1 切片操作 Book.objects.all()[0:8] 2 迭代操作 for obj in Book.objects.all(): print(obj.属性) 3 惰性查询 ret=Book.objects.all() # for i in ret: # print(i) # # Book.objects.create(title="java",publishDate="2012-12-12",price=12,publish_id=1) # # for i in ret: # print(i) 4 缓存机制 # ret=Book.objects.all().exists() # if ret: # print("OK") # ret=Book.objects.all().iterator() # print(type(ret)) # for i in ret: # print(i) # Book.objects.create(title="hello2",publishDate="2012-12-12",price=12,publish_id=1) # # for i in ret: # print(i) modle: model_name=Book._meta.model_name app_label=Book._meta.app_label title=Book._meta.get_field("title") print(title) print(type(title)) print(title.max_length) price=Book._meta.get_field("price") print(price) print(type(price)) print(price.max_digits) 自定义分页 查看页面 分页 search action filter pop
stark 分页 分页组件 保存搜索条件 search action filter: print("filter_field",filter_field) # "publish" filter_field_obj=self.config.model._meta.get_field(filter_field) print("filter_field_obj",filter_field_obj) print(type(filter_field_obj)) from django.db.models.fields.related import ForeignKey from django.db.models.fields.related import ManyToManyField print("rel...",filter_field_obj.rel.to.objects.all())
pop功能: 1 如何在一对多和多对多字段后渲染 + 2 +对应的跳转路径 3 保存添加记录同时,将原页面的对应的下拉菜单中添加该记录