django_day03
django_day03
Django的view(视图)
CBV和FBV
FBV:function based view 基于函数的视图
CBV:class based view 基于类的视图
from django.views import View class Xxx():
def get(self,request):
#专门处理get请求
return response
def post(self,request):
#专门处理post请求
return responseurl(r'xx/',Xxx.as_view())
class PublisherAdd(View): def get(self,request):
print("get请求被执行")
#处理get请求
return render(request, 'publisher_add.html') def post(self,request):
print("post请求被执行")
#处理post请求
pub_name = request.POST.get('pub_name')
#print(pub_name)
if not pub_name:
# 输入为空
return render(request, 'publisher_add.html', {'error': '不能为空!!'})
if models.Publisher.objects.filter(name=pub_name):
return render(request, 'publisher_add.html', {'error': '已存在!'})
ret = models.Publisher.objects.create(name=pub_name)
#print(ret, type(ret))
return redirect('/publisher_list/')
urlpatterns = [
url(r'^publisher_add/', views.PublisherAdd.as_view()),
]
as_view流程
项目运行时加载urls.py的文件 执行类as_view方法
as_view()执行后 内部定义了一个view函数 并且返回
请求到来的时候,执行view函数:
实例化类--》self
self.request = request
执行self.dispatch(request, *args, **kwargs)的方法
判断请求方式是否被允许
允许:
通过反射获取请求方式对应的请求方法 ——》 handler
获取不到 self.http_method_not_allowed ——》 handler
不允许:
self.http_method_not_allowed ——》 handler
执行handler,返回结果
from functools import wraps def timer(func):
@wraps(func)#不加的话获取的方法一直时inner wraps原理
def inner(request, *args, **kwargs): start = time.time()
ret = func(request, *args, **kwargs)
print('执行的时间是:{}'.format(time.time() - start))
return ret return inner
FBV
直接加在函数上就行
CBV加装饰器:
需要使用一个装饰器
from django.utils.decorators import method_decorator
加在方法上
@method_decorator(timer)
def get(self, request):
加在dispatch方法上
@method_decorator(timer)
def dispatch(self, request, *args, **kwargs):
# 之前的操作
ret = super().dispatch(request, *args, **kwargs) # 执行View中的dispatch方法
# 之后的操作
return ret3 @method_decorator(timer,name='dispatch')
class PublisherAdd(View):
加在类上
@method_decorator(timer,name='post')
@method_decorator(timer,name='get')
class PublisherAdd(View):
request
request.method 请求方法 GET POST
request.GET URL上携带的参数 ?k1=v1&k2=v2 { }
request.POST post请求提交的数据 {} 编码方式是URLencode
request.path_info 路径信息 不包含IP和端口 也不包含参数
request.body 请求体 bytes类型 #post请求才有 数据
request.COOKIES cookie
request.session session
request.FILES 长传的文件
request.META 头的信息 小写——》大写 HTTP_ 开头 - ——》 _ request.get_full_path() 完整的路径信息 不包含IP和端口 包含参数
request.is_ajax() 请求是否是ajax请求response
from django.shortcuts import render, HttpResponse, redirect HttpResponse('字符串') # 返回字符串
render(request,'模板的文件名',{'k1':v1}) # 返回一个HTML页面
redirect('地址') # 重定向 Location:‘地址’ 301 302
from django.http.response import JsonResponse
JsonResponse({'k1':'v1'})
JsonResponse(data,safe=False)
上传文件
urls.py
url(r'^upload/', views.Upload.as_view()),
视图:
class Upload(View): def get(self, request):
return render(request, 'upload.html')
def post(self, request):
file = request.FILES.get('f1')
with open(file.name, 'wb') as f:
for i in file:
f.write(i)
return HttpResponse('ok')
upload.html
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %} <input type="file" name="f1">
<button>上传</button>
</form>
django_day03的更多相关文章
随机推荐
- 在jupyter中配置c++内核
安装 xeus-cling conda install xeus-cling -c conda-forg xeus-cling 是一个用于编译解释于C++的Jupyter内核目前,支持Mac与Linu ...
- 基于.NET6的开源工业物联网网关
什么是工业物联网网关 工业物联网网关(IIoTGateway)是一种硬件设备或软件程序,作为本地设备(如PLC.扫码枪.机器人.数控机床.非标上位机等)与云端系统(如物联网平台.SCADA系统.MES ...
- 【图解源码】Zookeeper3.7源码分析,包含服务启动流程源码、网络通信源码、RequestProcessor处理请求源码
Zookeeper3.7源码剖析 能力目标 能基于Maven导入最新版Zookeeper源码 能说出Zookeeper单机启动流程 理解Zookeeper默认通信中4个线程的作用 掌握Zookeepe ...
- java单链表基本操作
/** * */ package cn.com.wwh; /** * @Description:TODO * @author:wwh * @time:2021-1-18 19:24:47 */ pub ...
- NC21874 好串
NC21874 好串 题目 题目描述 牛牛喜欢跟字符串玩耍,他刚刚学会了一个新操作,将一个字符串x插入另一个字符串y中(包括放在开头和结尾) 牛牛认为如果一个串是好的当这个串能按照如下方法被构造出来: ...
- CF1042E Vasya and Magic Matrix 题解
题目链接 思路分析 看到题目中 \(n,m \leq 1000\) ,故直接考虑 \(O(n^2)\) 级别做法. 我们先把所有的点按照 \(val\) 值从小到大排序,这样的话二维问题变成序列问题. ...
- labview入门到出家10(进阶)——CAN通讯
讲完串口,这边再讲一个labveiw工控程序中比较常用的CAN通讯吧.很久没有写过CAN通讯的程序了,网上一搜就是什么现场总线,控制器局域网总线,然后一堆复杂的协议.在这里还是一 ...
- freeswitch的话单模块
概述 最近因为业务需要,在看freeswitch中话单相关的一些模块. 在voip的使用过程中,话单是重要的基础模块,涉及到计费和问题查找. 呼叫话单最重要的一点是稳定,不能有错误或遗漏. 本章对fs ...
- 一文吃透如何部署kubernetes之Dashboard
kubernetes Dashboard是什么? Dashboard是kubernetes的Web GUI,可用于在kubernetes集群上部署容器化应用,应用排错,管理集群本身及其附加的资源等,它 ...
- 21条最佳实践,全面保障 GitHub 使用安全
GitHub 是开发人员工作流程中不可或缺的一部分.无论你去哪个企业或开发团队,GitHub 都以某种形式存在.它被超过8300万开发人员,400万个组织和托管超过2亿个存储库使用.GitHub 是世 ...