第05章节-Python3.5-Django的CBV和FBV 4

简介: 4. FBV & CBVfunction base viewurl.py index -> 函数名view.py def 函数(request): .

4. FBV & CBV

function base view
url.py
  index -> 函数名
view.py
  def 函数(request):
    ...
===>
/index/ -> 函数名
/index/ -> 类
===>

建议:两者都用

image.png
  • 修改urls.py:
"""s14day19_2 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
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),
    # views.Home.as_view()是固定用法
    url(r'^home/', views.Home.as_view()),
]

image.png
image.png
  • 新建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>
image.png
  • 修改views.py:
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.


def index(request):
    return HttpResponse('Index')


'''
def login(request):
    # 判断用户获取数据方式是GET,就返回什么数据
    if request.method == "GET":
        return render(request, 'login.html')
    # 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        if u == 'alex' and p == '123':
            return redirect('/index/')
        else:
            return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")

'''


def login(request):
    # 判断用户获取数据方式是GET,就返回什么数据
    if request.method == "GET":
        return render(request, 'login.html')
    # 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
    elif request.method == "POST":
        # radio
        # v = request.POST.get('gender')
        # print(v)
        # v = request.POST.getlist('favor')
        # print(v)
        v = request.POST.get('fff')
        print(v)
        # 所有上传文件都上传到request.FILES
        obj = request.FILES.get('fff')
        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/")


# def home(request):
#     return HttpResponse('Home')


from django.views import View


class Home(View):

    # 调用父类中的dispatch(相当于助理,)
    def dispatch(self, request, *args, **kwargs):
        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, 'POST')
        return render(request, 'home.html')

image.png
  • 效果图:
image.png
image.png
目录
相关文章
Django-视图CBV执行流程详解
FBV和CBV其实就是要么写函数要么写类,那么内部原理是怎么实现? FBV写法 def func_view(request): "Function views" if request.
1504 0
|
JSON 数据格式 Python
Django之CBV
CBV,即Class Base View,类基本视图。 在写API时,我们通常都是使用CBV,而非FBV (Function Base View)。 1.
1433 0
|
Web App开发 Go Python
源码解析Django CBV的本质
Django CBV模式的源码解析 通常来说,http请求的本质就是基于Socket Django的视图函数,可以基于FBV模式,也可以基于CBV模式。 基于FBV的模式就是在Django的路由映射表里进行url和视图函数的关联,而基于CBV的模式则是在views.
1379 0
|
数据安全/隐私保护 Python
|
Python
如何在DJANGO里获取?带数据的东东,基于CBV
用DEF的,有现成的,而用CLASS的,就要作一下变通。 如下: if self.request.GET: if self.request.GET.get('search_pk'): search_pk = self.
944 0
|
5天前
|
前端开发 UED Python
Wagtail-基于Python Django的内容管理系统CMS实现公网访问
Wagtail-基于Python Django的内容管理系统CMS实现公网访问