Python系统学习-19

##1. 路由系统进阶:
https://www.cnblogs.com/maple-shaw/articles/9282718.html
- 动态路由
- URL命名和反向解析
url(r’^publisher_list/$’, views.publisher_list, name=‘publisher’),

		命名分组
		url(r'^pub/edit/(?P<pk>\d+)/$', views.publisher_edit,name='publisher_edit'),
		
		模板中使用:
			{% url 'publisher'%}   ——》/app01/publisher_list/
			
			命名分组
			{% url 'publisher_edit' publisher.id %}
			{% url 'publisher_edit' pk=publisher.id %}
			
		视图中使用:
			from django.urls import reverse
			reverse('publisher')
			
			命名分组
			reverse('publisher_edit',args=(2,))
			reverse('publisher_edit',kwargs={'pk':5})
			
			
		指定namespace='app01'  
			反向解析的时候  给name前面加上namespace   ——》     namespace:name  
			
			
		
2. 视图函数进阶: https://www.cnblogs.com/maple-shaw/articles/9285269.html
	- FBV  (function based view) 
	
		
	- CBV (class based view)
	
		定义:
			class AddPublisher(View):
	
				def get(self,request):
					pass
				
				def post(self,request):
					pass
				
		使用:
			  url(r'^pub/add/$', views.AddPublisher.as_view(),name='publisher_add'),

		
		CBV的流程
			1. AddPublisher.as_view()  ——》 view函数
			2. 请求到来的时候 执行view函数
				1. AddPublisher实例化 对象  ——》 self 
				2. self.request = request
				3. 执行self.dispatch(request, *args, **kwargs)
					1. 通过反射 获取到 get  post 方法  ——>handler
				
					2. handler()  ——》  获得HttpResponse对象
			
		装饰器的使用:
			FBV: 正常使用 给函数上加装饰器
			CBV:
				from django.utils.decorators import method_decorator
				
				1. 加在方法上
				    @method_decorator(timer)
					def get(self, request):
					
				2. 加在dispatch方法上
					@method_decorator(timer)
					def dispatch(self, request, *args, **kwargs):
					
				3. 加在类上
					@method_decorator(timer,name='post')
					@method_decorator(timer,name='get')
					class AddPublisher(View):
					
			1. 直接用装饰器
				(<app01.views.AddPublisher object at 0x0000023DE99E57B8>, <WSGIRequest: GET '/app01/pub/add/'>)
				<function AddPublisher.get at 0x0000023DE992ED08>	
			2. 使用method_decorator
				(<WSGIRequest: GET '/app01/pub/add/'>,)
				<function method_decorator.<locals>._dec.<locals>._wrapper.<locals>.bound_func at 0x0000023372154EA0>
					
	- request
		request.method 请求方式  GET POST  
		request.GET    URL上的参数
		request.POST   POST请求提交的数据
		request.path_info    路径信息  不包含ip和端口 不包含URL参数  /app01/pub_list/
		request.FILES    上传的文件
		
		request.get_host()  IP:端口
		request.get_full_path()  带参数的路径
		
	- response
		HttpResponse('字符串')
		render(request,'html文件名',{})    完整的页面
		redirect(地址)  '/zz/'   'https://v3.bootcss.com/css/#forms'    Location :'/zz/'
		
		from django.http import JsonResponse
		JsonResponse    # Content-Type: application/json
		JsonResponse(data_list,safe=False)   传非字典类型
		
	
模板引擎进阶: https://www.cnblogs.com/maple-shaw/articles/9333821.html
	- 标签
	- 过滤器
	- 模板
	
		1. 把多个页面公共的部分提取出来 写成一个母版  HTML文件  'base.html'
			定义block 
			
		2. 写子页面
			{% extends 'base.html' %}
			重写母版中定义的block块 
			
		3. 注意事项:
			1. {% extends 'base.html' %}写在第一行,'base.html'是字符串
			2. 在母版中定义多个block块  css js
			3. 子页面的内容写在block中
			
	- CSRF
		CSRF 跨站请求伪造 
		Django有跨站请求伪造的保护机制 依赖中间件
			 'django.middleware.csrf.CsrfViewMiddleware',
			 
		{% csrf_token %}
		
		效果:
			在form表中加了一个隐藏的input标签  name csefmiddlewaretoken  value  64长度的字符串
		结果:
			form表单可以提交POST请求
			
	- 静态文件相关
		- {% load static %}
		
		<link rel="stylesheet" href="{% static 'bootstrap-3.3.7/css/bootstrap.css' %}"
		<link rel="stylesheet" href="{% static 'css/dsb.css' %}">}

		<link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7/css/bootstrap.css">
		<link rel="stylesheet" href="{% get_static_prefix %}css/dsb.css">
		
			

ORM单表查询13条+外键操作(一对多):
	字段和参数:  https://www.cnblogs.com/maple-shaw/articles/9323320.html
	查询操作:https://www.cnblogs.com/maple-shaw/articles/9403501.html

cookie&session:https://www.cnblogs.com/maple-shaw/articles/9502602.html

猜你喜欢

转载自blog.csdn.net/weixin_41765871/article/details/83035414
今日推荐