Django_request学习

Django_request

(1)请求方式

image-20220812115203856

这里使用一个接口测试软件postman

可以看到里面有非常多的发起请求的方式,最常用的就是GETPOST请求,但是这些方法无法在网页的url里显示

image-20220828134900227

在学习request参数之前,django框架中首先接到浏览器发来的请求第一站是经过框架自带的wsgi.py文件

"""
WSGI config for djangoRegina project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoRegina.settings")

application = get_wsgi_application()

这个文件的作用就是接收浏览器数据,进行数据解析,并按照http协议的请求格式进行封装传递给路由分发部分

(2)请求内容

def ZJR_request(request):
    #请求格式
    print(request.method)
    #请求体
    print(request.body)

image-20220828141700116

因为在get请求当中并没有参数设置,所以得到的请求体为空。如果需要加入请求体的内容,那么需要改为post请求

错误修正

  1. url错误

    image-20220828143958216

    如果发生这种错误,是因为没有在url末尾写全,必须以/结尾

    image-20220828144237585

  2. csrf

    image-20220828144313540

    CSRF # 表示django全局发送post请求均需要字符串验证
    功能:防止跨站请求伪造的功能
    工作原理:客服端访问服务器,在服务端正常返回给客户端数据的时候,而外返回给客户端一段字符串,等到客户端下次访问服务器时,服务器会到客户端查找先前返回的字符串,如果找到则继续,找不到就拒绝。
    访问流程:客户端 —> URL路由系统—> CSRF—> 视图函数
    需要在客户端页面的post表单中添:{% csrf_token%}
    setting.py里的"django.middleware.csrf.CsrfViewMiddleware"注释掉就可以了

    image-20220828144658919

image-20220828144711470

此时得到了刚刚提交的以urlencoded方式的数据,这种格式就是将普通的键值对变为用&符号连接的字符串

常用的还有通过json格式传输的键值对,选择raw选项,并在下来菜单中选择json

image-20220828145413980

这里面的字符必须使用双引号

image-20220828145557116

request.post

这个方法也是输出请求体的一种,但是需要注意的是它并不适用每一种传参格式

image-20220828154009322

这是通过urlencoded格式

image-20220828154056662

这是json格式

获取值

 name = request.POST.get("name")
 age = request.POST.get("age")
 print(name,age)

这个是必须通过urlencoded格式传参,然后通过POST方法得到具体的值

image-20220828154723900

获取值列表

image-20220828155331131如果在传参的时候一个关键字包含了多个内容,就需要getlist方法

name = request.POST.getlist("name")

image-20220828155316860

如果只用get方法,按照django自己的逻辑,只能选去这个关键字的最后一个选项

image-20220828155449219

get请求

在postman里,get请求的参数要在param里面添加

image-20220828155815454

image-20220828155854620

获取路径

print(request.path)
    print(request.get_full_path())

image-20220828160415942

如果get请求中添加了参数,那么get_full_path方法则会取到所有参数,但path方法则不会

但如果get请求里没有添加参数,两者的输出结果是一样的

image-20220828160530877

获取请求头

request.META

通过这种方法挥获得一大堆的键值对,通过解析可以获取到格式化后的内容

{
	"PATH": "/Users/ivanlee/opt/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/Users/ivanlee/.dotnet/tools:/usr/local/mysql/bin",
	"MANPATH": "/usr/local/share/man::",
	"CONDA_EXE": "/Users/ivanlee/opt/anaconda3/bin/conda",
	"CONDA_PYTHON_EXE": "/Users/ivanlee/opt/anaconda3/bin/python",
	"HOMEBREW_PREFIX": "/usr/local",
	"COMMAND_MODE": "unix2003",
	"_CE_M": "",
	"LOGNAME": "ivanlee",
	"HOMEBREW_REPOSITORY": "/usr/local/Homebrew",
	"XPC_SERVICE_NAME": "application.com.jetbrains.pycharm.ce.24716539.38684274",
	"PWD": "/Users/ivanlee/Desktop/djangoRegina",
	"PYCHARM_HOSTED": "1",
	"INFOPATH": "/usr/local/share/info:",
	"CONDA_SHLVL": "0",
	"__CFBundleIdentifier": "com.jetbrains.pycharm.ce",
	"PYTHONPATH": "/Users/ivanlee/Desktop/djangoRegina",
	"SHELL": "/bin/zsh",
	"PYTHONIOENCODING": "UTF-8",
	"HOMEBREW_BOTTLE_DOMAIN": "https://mirrors.ustc.edu.cn/homebrew-bottles",
	"OLDPWD": "/",
	"HOMEBREW_CELLAR": "/usr/local/Cellar",
	"USER": "ivanlee",
	"TMPDIR": "/var/folders/48/byh7qq5528sfmw6vgsxnd9g80000gn/T/",
	"SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.qyJ40GUnAv/Listeners",
	"_CE_CONDA": "",
	"XPC_FLAGS": "0x0",
	"PYTHONUNBUFFERED": "1",
	"__CF_USER_TEXT_ENCODING": "0x1F5:0x0:0x0",
	"LC_CTYPE": "en_US.UTF-8",
	"HOME": "/Users/ivanlee",
	"DJANGO_SETTINGS_MODULE": "djangoRegina.settings",
	"TZ": "UTC",
	"RUN_MAIN": "true",
	"SERVER_NAME": "1.0.0.127.in-addr.arpa",
	"GATEWAY_INTERFACE": "CGI/1.1",
	"SERVER_PORT": "8090",
	"REMOTE_HOST": "",
	"CONTENT_LENGTH": "24",
	"SCRIPT_NAME": "",
	"SERVER_PROTOCOL": "HTTP/1.1",
	"SERVER_SOFTWARE": "WSGIServer/0.2",
	"REQUEST_METHOD": "POST",
	"PATH_INFO": "/JRrequest/jr/",
	"QUERY_STRING": "name=regina",
	"REMOTE_ADDR": "127.0.0.1",
	"CONTENT_TYPE": "application/x-www-form-urlencoded",
	"HTTP_USER_AGENT": "PostmanRuntime/7.29.2",
	"HTTP_ACCEPT": "*/*",
	"HTTP_POSTMAN_TOKEN": "e474691f-e449-4c99-b8ff-d6c625b6c5b6",
	"HTTP_HOST": "127.0.0.1:8090",
	"HTTP_ACCEPT_ENCODING": "gzip, deflate, br",
	"HTTP_CONNECTION": "keep-alive",
	"wsgi.input": < django.core.handlers.wsgi.LimitedStream object at 0x7feafca4d160 > ,
	"wsgi.errors": < _io.TextIOWrapper name = "<stderr>"
	mode = "w"
	encoding = "utf-8" > ,
	"wsgi.version": (1, 0),
	"wsgi.run_once": False,
	"wsgi.url_scheme": "http",
	"wsgi.multithread": True,
	"wsgi.multiprocess": False,
	"wsgi.file_wrapper": < class "wsgiref.util.FileWrapper" >
}

还可以从这里进行获取

print(request.META.get("HTTP_HOST"))

image-20220828230733365

自定义请求头

image-20220828230937495

这里在最后一行添加了自定义的一对键值对,提交以后我们可以看到META内容里的经过格式化的信息

image-20220828231125486

同样也可以通过get方式获得,格式写成HTTP开头

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Django_request学习