Python Day22
http协议常用的请求方式
get请求:查询
post请求:提交数据(添加记录)
delete请求:删除数据
put/patch:更新数据
Django的CBV
FBV与CBV
FBV(function base views) 就是在视图里使用函数处理请求。
CBV(class base views) 就是在视图里使用类处理请求。
Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承、封装、多态)。所以Django在后来加入了Class-Based-View。可以让我们用类写View。这样做的优点主要下面两种:
提高了代码的复用性,可以使用面向对象的技术,比如Mixin(多继承)
可以用不同的函数针对不同的HTTP方法处理,而不是通过很多if判断,提高代码可读性
语法
urls
from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/', views.LoginView.as_view()), ]
views
from django.views import View class LoginView(View): #这样写的意义在于无论什么请求都要执行的操作就可以写在下面的函数中 def dispatch(self, request, *args, **kwargs): print("OK") #调用父类中的dispatch ret = super().dispatch(request, *args, **kwargs) return ret def get(self, request): return render(request, "login.html") def post(self, request): user = request.POST.get("user") return HttpResponse("OK")
解读源码
执行过程
url(r'^login/', views.LoginView.as_view())
url匹配后执行的是as_view这个函数,我们要先看看这个函数
LoginView中并没有这个as_view函数,所以我们要去LoginView的父类View中去查找
在View类中我们发现as_view函数最后会return一个叫view的函数
所以,此时url匹配成功后执行的就是这个view函数而view函数又会返回一个dispatch的方法(如下)
def dispatch(self, request, *args, **kwargs): # Try to dispatch to the right method; if a method doesn't exist, # defer to the error handler. Also defer to the error handler if the # request method isn't on the approved list. if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed) else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs)
dispatch返回的是什么,就是view返回的是什么
通过上的例子我们可以看到,如果是get访问就会找到LoginView类中的get函数
其实到最后还是FBV事实上,利用这一特点(dispatch方法),我们可以在CBV类中自己定义一个dispatch函数(如上例),来增加一些想要增加的功能
admin
Django后台数据库管理工具(web页面)
admin组件使用
使用admin功能需要先初始化数据库,创建超级用户
class BookConfig(admin.ModelAdmin): #list_display,列表时,定制显示的列 list_display = ["nid","title","price","publishDate","publish"] #list_display_links,列表时,定制列可以点击跳转 list_display_links=["title","price"] #list_filter,列表时,定制右侧快速筛选。 list_filter=["title","publish"] #search_fields,列表时,模糊搜索的功能 search_fields=["title"] #action,列表时,定制action中的操作 #定制Action行为具体方法 def patch_init(self,request,queryset): queryset.update(price=100) #中文显示自定义Actions patch_init.short_description = "批量初始化" actions = [patch_init] #change_list_template="list.html" #ordering,列表时,数据排序规则 ordering=("price","nid",)
#需要定制哪些表
admin.site.register(Book,BookConfig) admin.site.register(Publish) admin.site.register(Author) admin.site.register(AuthorDetail)
参考:
https://×××w.cnblogs.com/yuanchenqi/articles/8323452.html#_label0
- Python之路,Day22 - 网站用户访问质量分析监测分析项目开发
- python Day22 linux基础8
- day22 python学习随笔
- Python笔记day22(对象)|初识面向对象
- python学习—Day22—python的正则表达式
- python,day22-Django之Form组件,信号
- 复习PythonDay22:一些经验之谈
- python_day22_Django-7 (博客-富文本编辑器)
- python,day22-sessin
- python 学习 更改照片大小 DAY22
- 路飞学城Python-Day22
- 使用python实现标记图像感兴区域
- python - warnings
- PHP、Java、.NET、Python、Ruby资源大集锦
- Python环境部署
- Upgrade Python And Pip On CentOS 6.x
- python大小写转换
- 应用于Python的vim配置点滴
- python导出邮箱里的联系人,支持Gmail等
- Python 中math模块详解