Django 视图 Views *****

文章对应的B站视频:https://www.bilibili.com/video/BV1Tu41127Ca/

Django系列文章对应的目录:https://www.cnblogs.com/emanlee/p/15860241.html

Django 视图

视图层

一个视图函数,简称视图,是一个简单的 Python 函数,它接受 Web 请求并且返回 Web 响应。

响应可以是一个 HTML 页面、一个 404 错误页面、重定向页面、XML 文档、或者一张图片...

无论视图本身包含什么逻辑,都要返回响应。代码写在哪里都可以,只要在 Python 目录下面,一般放在项目的 views.py 文件中。

每个视图函数都负责返回一个 HttpResponse 对象,对象中包含生成的响应。

视图层中有两个重要的对象:请求对象(request)与响应对象(HttpResponse)。


请求对象: HttpRequest 对象(简称 request 对象)

以下介绍几个常用的 request 属性。

1、GET

数据类型是 QueryDict,一个类似于字典的对象,包含 HTTP GET 的所有参数。

有相同的键,就把所有的值放到对应的列表里。

取值格式:对象.方法。

get():返回字符串,如果该键对应有多个值,取出该键的最后一个值。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py

from django.http import HttpResponse

def mydef(request):
    name = request.GET.get("name")
    return HttpResponse('姓名:{}'.format(name))
访问:
127.0.0.1:8003/mytemp/?name=我的名字

 

 

 

2、POST

数据类型是 QueryDict,一个类似于字典的对象,包含 HTTP POST 的所有参数。

常用于 form 表单,form 表单里的标签 name 属性对应参数的键,value 属性对应参数的值。

取值格式: 对象.方法。

get():返回字符串,如果该键对应有多个值,取出该键的最后一个值。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py

from django.http import HttpResponse

def mydef(request):
    name = request.POST.get("name")
    return HttpResponse('姓名:{}'.format(name))

  Postman  测试:

 

 

 

 

3、body

数据类型是二进制字节流,是原生请求体里的参数内容,在 HTTP 中用于 POST,因为 GET 没有请求体。

在 HTTP 中不常用,而在处理非 HTTP 形式的报文时非常有用,例如:二进制图片、XML、Json 等。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py

from django.http import HttpResponse

def mydef(request):
    name = request.body
    print(name)
    return HttpResponse("姓名")

 

  Postman  测试:
 

cmd窗口输出结果:

b'----------------------------596034534812480288644101\r\nContent-Disposition: form-data; name="name"\r\n\r\n\xe6\x88\x91\xe7\x9a\x84\xe5\xa7\x93\xe5\x90\x8d\r\n----------------------------596034534812480288644101--\r\n'
D:\temp-test\djangodemo\mysite456\mysite456\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 29, 2022 - 15:17:21
Django version 4.0.1, using settings 'mysite456.settings'
Starting development server at http://127.0.0.1:8003/
Quit the server with CTRL-BREAK.
b'----------------------------596034534812480288644101\r\nContent-Disposition: form-data; name="name"\r\n\r\n\xe6\x88\x91\xe7\x9a\x84\xe5\xa7\x93\xe5\x90\x8d\r\n----------------------------596034534812480288644101--\r\n'
[29/Jan/2022 15:17:49] "POST /mytemp/ HTTP/1.1" 200 6

 

 

 

4、path

获取 URL 中的路径部分,数据类型是字符串。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py
from django.http import HttpResponse

def mydef(request):
    name = request.path
    print(name)
    return HttpResponse("姓名")

  Postman  测试:

 

 cmd窗口输出结果:

System check identified no issues (0 silenced).
January 29, 2022 - 15:23:11
Django version 4.0.1, using settings 'mysite456.settings'
Starting development server at http://127.0.0.1:8003/
Quit the server with CTRL-BREAK.
/mytemp/
[29/Jan/2022 15:23:57] "POST /mytemp/ HTTP/1.1" 200 6

 

 

5、method

获取当前请求的方式,数据类型是字符串,且结果为大写。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py
from django.http import HttpResponse

def mydef(request):
    name = request.method
    print(name)
    return HttpResponse("姓名")

 

 

  cmd窗口输出结果:

System check identified no issues (0 silenced).
January 29, 2022 - 15:25:39
Django version 4.0.1, using settings 'mysite456.settings'
Starting development server at http://127.0.0.1:8003/
Quit the server with CTRL-BREAK.
POST
[29/Jan/2022 15:26:21] "POST /mytemp/ HTTP/1.1" 200 6

 

 


响应对象:HttpResponse 对象

响应对象主要有三种形式:HttpResponse()、render()、redirect()。

HttpResponse():

返回文本,参数为字符串,字符串中写文本内容。如果参数为字符串里含有 html 标签,也可以渲染。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

views.py
from django.http import HttpResponse

def mydef(request):
    return HttpResponse("<a href='https://www.cnblogs.com/emanlee/'>教程</a>")

 

 

 

 

render():

返回文本,第一个参数为 request,第二个参数为字符串(页面名称),第三个参数为字典(可选参数,向页面传递的参数:键为页面参数名,值为views参数名)。

实例

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 views.py

from django.shortcuts import render

def mydef(request):
    name ="教程"
    return render(request,"mytemp.html",{"name":name})
mytemp.html
{{ name|lower }}

 

 

 

 

redirect()

重定向,跳转新页面。参数为字符串,字符串中填写页面路径。一般用于 form 表单提交后,跳转到新页面。

实例

 

urls.py

 

from django.urls import path
from . import views

urlpatterns = [
    path('mytemp/', views.mydef),
]

 

 views.py

from django.shortcuts import redirect

def mydef(request):
    return redirect("/index/")

render 和 redirect 是在 HttpResponse 的基础上进行了封装:

    • render:底层返回的也是 HttpResponse 对象
    • redirect:底层继承的是 HttpResponse 对象

 

 

 

继续阅读,请看总目录:

Django系列文章对应的目录:https://www.cnblogs.com/emanlee/p/15860241.html

 

REF

https://www.runoob.com/django/django-views.html

 

posted @ 2022-01-29 15:36  emanlee  阅读(237)  评论(0编辑  收藏  举报