码迷,mamicode.com
首页 > 其他好文 > 详细

post请求

时间:2019-12-20 20:40:21      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:请求   setting   登录   with   coding   else   you   img   span   

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登录失败")
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跨域

post请求

标签:请求   setting   登录   with   coding   else   you   img   span   

原文地址:https://www.cnblogs.com/hy-74/p/12074818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!