post请求
Posted hy-74
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了post请求相关的知识,希望对你有一定的参考价值。
post请求在二级目录下的views.py文件中写入
from django.http import HttpResponse # Create your views here. a = {"name":"来了老弟","pwd":"老弟你真棒"} #声明全局变量 def indexb(request): if request.method == "POST": name = request.POST.get("name") #post请求 pwd = request.POST.get("pwd") if name == a["name"] and pwd == a[‘pwd‘]: #判断 return HttpResponse("POST登陆成功") else: return HttpResponse("POST登录失败")
打开postman软件
post请求需要跨域需要在一级目录下的settings.py文件中写入
下包 pip install django-cors-headers 下面的操作在setting里 添加到appps里 INSTALLED_APPS = [ ... ‘corsheaders‘, ... ] 注释掉csrf并添加‘corsheaders.middleware.CorsMiddleware‘ MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, # ‘django.middleware.csrf.CsrfViewMiddleware‘, ‘django.contrib.auth.middleware.AuthenticationMiddleware‘, ‘django.contrib.messages.middleware.MessageMiddleware‘, ‘django.middleware.clickjacking.XFrameOptionsMiddleware‘, ‘corsheaders.middleware.CorsMiddleware‘, ] 添加跨域配置 CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_WHITELIST = () CORS_ALLOW_METHODS = ( ‘DELETE‘, ‘GET‘, ‘OPTIONS‘, ‘PATCH‘, ‘POST‘, ‘PUT‘, ‘VIEW‘, ) CORS_ALLOW_HEADERS = ( ‘XMLHttpRequest‘, ‘X_FILENAME‘, ‘accept-encoding‘, ‘authorization‘, ‘content-type‘, ‘dnt‘, ‘origin‘, ‘user-agent‘, ‘x-csrftoken‘, ‘x-requested-with‘, ‘Pragma‘, )
以上是关于post请求的主要内容,如果未能解决你的问题,请参考以下文章
求教golang中http发送post请求gb2312编码的解决方案
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装