FBV:

Function Base View,基于函数的视图

views.py:

from django.shortcuts import render, HttpResponse

# FBV
def upload(request):
if request.method == "POST":
filename = request.FILES["upload_file"].name
with open(filename, "wb") as f:
for chunk in request.FILES["upload_file"].chunks():
f.write(chunk)
return HttpResponse("上传OK")
else:
return render(request, "upload.html")

urls.py:

from django.conf.urls import url
from app01 import views urlpatterns = [
url(r'^upload/', views.upload), # FBV
]

CBV:

Class Base View,基于类的视图

views.py:

from django.shortcuts import render, HttpResponse
from django.views import View # CBV 需要导入 # CBV
class Upload(View):
# 定义 get 函数,用于处理 get 请求
def get(self, request):
return render(request, "upload.html") # 定义 post 函数,用于处理 post 请求
def post(self, request):
filename = request.FILES["upload_file"].name
with open(filename, "wb") as f:
for chunk in request.FILES["upload_file"].chunks():
f.write(chunk)
return HttpResponse("上传OK")

urls.py:

from django.conf.urls import url
from app01 import views urlpatterns = [
# url(r'^upload/', views.upload), # FBV
url(r'^upload/', views.Upload.as_view()), # CBV
]

Python - Django - FBV 和 CBV的更多相关文章

  1. python 视图 (FBV、CBV ) 、Request 和Response对象 、路由系统

    一.FBV和CBV1.基于函数的view,就叫FBV(Function Based View) 示例: def add_book(request): pub_obj=models.Publisher. ...

  2. django——FBV与CBV

    引言 FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class bas ...

  3. Django FBV和CBV -

    一.FBV和CBV 在Python菜鸟之路:Django 路由.模板.Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view ...

  4. Python/Django(CBV/FBV/ORM操作)

    Python/Django(CBV/FBV/ORM操作) CBV:url对应的类(模式) ##====================================CBV操作============ ...

  5. python 全栈开发,Day84(django请求生命周期,FBV和CBV,ORM拾遗,Git)

    一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...

  6. Python菜鸟之路:Django 路由补充1:FBV和CBV - 补充2:url默认参数

    一.FBV和CBV 在Python菜鸟之路:Django 路由.模板.Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view ...

  7. django的FBV和CBV

    title: python djano CBV FBV tags: python, djano, CBV, FBV grammar_cjkRuby: true --- python django的fu ...

  8. django请求生命周期,FBV和CBV,ORM拾遗,Git

    一.django 请求生命周期 流程图: 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post, ...

  9. Django之FBV与CBV

    一.FBV与CBV FBV(function based views),即基于函数的视图:CBV(class based views),即基于类的视图,也是基于对象的视图.当看到这个解释时,我是很萌的 ...

随机推荐

  1. Python通过logging记录日志并应用coloredlogs在控制台输出有色字体

    import logging import os from logging.handlers import TimedRotatingFileHandler import coloredlogs # ...

  2. fastjson ping外带信息poc

    public class Exploit { public Exploit(){ String base_url = ".egpkd5.dnslog.cn"; //你的dnslog ...

  3. python根据字典的值进行排序:

    有一个列表嵌套字典:[{"a": 5}, {"b": 4}, {"c": 1},{"e": 2}, {"d&q ...

  4. (尚007)Vue强制绑定class和style

    注意:class和style的值是动态的值 1.test007.html <!DOCTYPE html><html lang="en"><head&g ...

  5. C用malloc 向系统申请一个大小为n*4个字节的内存块

    #include <stdio.h> #include <malloc.h> void out(int *p, int n){ int i; for(i=0;i<n;i+ ...

  6. C语言for 循环 9*9 实现九九乘法表

    #include <stdio.h> int main(void) { //for循环实现9*9乘法表 /* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 */ ...

  7. 45、sparkSQL UDF&UDAF

    一.UDF 1.UDF UDF:User Defined Function.用户自定义函数. 2.scala案例 package cn.spark.study.sql import org.apach ...

  8. mysql设置主键自增长和自增长初始值

          本文主要向大家介绍MySQL数据库之Mysql创建表实现主键自增并且初始值为200,希望对大家学习MySQL数据库有所帮助.       假设已经创建表test,并且主键为id.Mysql ...

  9. ICEM-气化炉

    原视频下载地址:https://yunpan.cn/cuPJWRHUJKXIL  访问密码 d379

  10. leetcode 61. 旋转链表

    题目描述: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输 ...