zoukankan      html  css  js  c++  java
  • Django的CBV和FBV

    1. Django支持两种对应关系,CBV和FBV

    FBV:function base view &CBV:class base view
      url.py
       index---函数名
      views.py
       def 函数(request):
      

    /index/ -> 函数名
     /index/ ->类,请求来了以后,会执行类里面的指定的方法。

    2.CBV提交----- class Home(view) Home类必须继承view.

    2.1 urls.py中写对应关系

    2.2 views.py中写函数. get来了,执行get方法, post来了,执行 post方法。

    2.3 home.html中写模板

     3. 过程

    服务器端先运行起来; 客户端发来请求,先去匹配URL,找到类。再进行二次匹配,匹配请求的方法(get/put)。

    是基于反射(getattr, hasattr, deleteattr)来进行匹配的,进而知道应该执行哪个类。

    父类的dispatch 是先执行的,后面的get/post 都是通过dispatch反射进而找到的。

    找到执行完以后,也是把结果先返回给dispatch,然后再返回给客户端。

    4. 自己可以修改父类的dispatch方法,进而增加一些新的功能。

    运行以后,后台结果如下:

    5.本节笔记

    FBV:function base view &CBV:class base view
    		url.py 
    			index---函数名
    		views.py 
    			def 函数(request):
    		/index/ -> 函数名
    		/index/ ->类,请求来了以后,会执行类里面的指定的方法。
    			
    		建议:两者都用
    

     6.至此

    urls.py程序:

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^index/', views.index),
        url(r'^login/', views.login),
        url(r'^home/', views.Home.as_view()),
    ]
    

     views.py程序

    from django.shortcuts import render,HttpResponse,redirect
    
    # Create your views here.
    def index(request):
        return HttpResponse('index')
    def login(request):
        if request.method=='GET':
            return render(request,'login.html')
        elif request.method=='POST':
            #radio
            #v1=request.POST.get('gender')
            #print(v1)
            #checkbox
            #v2=request.POST.getlist('favor')
            #print(v2)
            #v3=request.POST.get('fafafa')
           #print(v3)
            obj=request.FILES.get('fafafa')
            print(obj,type(obj),obj.name)
            import os
            file_path=os.path.join('upload',obj.name)
            f=open(file_path, mode="wb")
            for i in obj.chunks():
                f.write(i)
            f.close()
    
            return render(request,'login.html')
        else:
            # put,delete,head,option.....
            return redirect('/index/')
    
    
    from django.views import View
    class Home(View):
        def dispatch(self,request,*args,**kwargs):
            #调用父类中的dispatch
            print('before')
            result=super(Home,self).dispatch(request,*args,**kwargs)
            print('after')
            return result
    
        def get(self,request):
            print(request.method)
            return render(request,'home.html')
        def post(self,request):
            print(request.method)
            return render(request,'home.html')
    
    
    """def login(request):
        if request.method=='GET':
            return render(request,'login.html')
        elif request.method=='POST':
            u = request.POST.get('user')
            p = request.POST.get('pwd')
            if u=='root' and p=='123':
                return redirect('/index/')
            else:
                return render(request,'login.html')
        else:
            # put,delete,head,option.....
            return redirect('/index/')
    """
    

    home.html程序

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <form action="/home/" method="POST">
            <input type="text" name="user"/>
            <input type="submit"/>
        </form>
    </body>
    </html>
    
  • 相关阅读:
    git 命令速查及使用
    Centos6.5 LAMP环境源码包安装与配置,附安装包百度网盘地址 (转做笔记)
    不再为Apache进程淤积、耗尽内存而困扰((转))
    centos6.5 安装linux 环境
    window 配置wnmp(转下整理 ,全)
    mac下安装 xampp 无法启动apache (转,留用)
    Git命令行(转用于学习和记录)
    apache 局域网访问
    华为云GaussDB(for opengauss)如何绑定公网,实现putty的远程访问gaussdb数据库。
    Day9 打卡acwing.429 奖学金
  • 原文地址:https://www.cnblogs.com/momo8238/p/7511861.html
Copyright © 2011-2022 走看看