django获取请求参数及跨域解决方案

django获取请求参数

1.获取URL路径中的参数

需求:假设用户访问127.0.0.1/user/1/2,你想获取1,2。应该怎么操作呢?

(1)未命名参数(位置参数)

# 在项目下的urls.py下增加设置:

url(r'^user/(\d+)/(\d+)$',views.index)

# 在user.views的index视图中:

def index(request,a,b):# 接受的参数按顺序的

     returnHttpResponse("获得数据 %s %s"%(a,b))


(2)命名参数(关键字参数)

# 在项目下的urls.py下增加设置:

url(r'^user/(?P<category>\d+)/(?P<id>\d+)$',views.index)

# 在user.views的index视图中:

def  index(request,id,category):# 接受的参数可以不用按顺序的

      returnHttpResponse("获得数据 %s %s"%(category,id))


输出结果均是  获得数据 1 2


2.获取查询字符串

需求:获取127.0.0.1:8000/user?id=1&pid=99的查询字符串的值

# 在项目下的urls.py下增加设置:url(r'^user/$',views.index)

# 在user.views的index视图中:

def index(request):

    id = request.GET.get("id")

    pid = request.GET.get("pid")

    returnHttpResponse("获得数据 %s %s"%(id,pid))

注意:查询字符串的获取与请求方式无关:不管是GET还是POST请求,都可以通过request.GET属性来获取!!!


3.获取表单数据

用postman发送一个表单请求。

def index(request):

    id = request.POST.get("id")

    pid = request.POST.get("pid")

    returnHttpResponse("获得数据 %s %s"%(id,pid))

注意:request.POST只能用来获取POST方式的请求体表单数据!


4.获取非表单类型

request.body属性:获取非表单类型的请求体数据,如:JSON、XML等,获取到的数据类型为bytes类型

获取数据后,自己解析数据取出参数

def index(request):

    json_str = request.body

    json_str = json_str.decode()#python3.6及以上不用这一句代码dict_data =json.loads(json_str)  # loads把str转换为dict,dumps把dict转换为strid= dict_data.get("id")

    pid = dict_data.get("pid")

    returnHttpResponse("获得数据 %s %s"%(id,pid))


5.获取请求头的内容

常见的请求头如下:

CONTENT_LENGTH– The length of the request body (as a string).

CONTENT_TYPE– The MIME type of the request body.

HTTP_ACCEPT– Acceptable content types for the response.

HTTP_ACCEPT_ENCODING– Acceptable encodings for the response.

HTTP_ACCEPT_LANGUAGE– Acceptable languages for the response.

HTTP_HOST– The HTTP Host header sent by the client.

HTTP_REFERER– The referring page, if any.

HTTP_USER_AGENT– The client’s user-agent string.

QUERY_STRING– The query string, as a single (unparsed) string.

REMOTE_ADDR– The IP address of the client.

REMOTE_HOST– The hostname of the client.

REMOTE_USER– The user authenticated by the Web server, if any.

REQUEST_METHOD– A string such as"GET"or"POST".

SERVER_NAME– The hostname of the server.

SERVER_PORT– The port of the server (as a string).

获取请求头内容的用META

示例:

def index(request):

    ip = request.META.get("REMOTE_ADDR")

    returnHttpResponse("你的ip地址是%s"%ip)


6.获取自定义请求头的内容

用postman增加一个自定义的请求头,key=id,value=1。那么应该怎么取呢?

代码如下:

def index(request):

    id = request.META.get("HTTP_ID")

    returnHttpResponse("你的id:%s"%id)

注意:获取自定义的请求头属性值时,需要添加前缀HTTP_并转成大写,作为键来获取值

Python django解决跨域请求的问题

解决方案

1.安装django-cors-headers

pip3 install django-cors-headers

2.配置settings.py文件

INSTALLED_APPS = [

    ...

    'corsheaders',

    ...

]

MIDDLEWARE_CLASSES = (

    ...

    'corsheaders.middleware.CorsMiddleware',

    'django.middleware.common.CommonMiddleware',# 注意顺序    ...

)#跨域增加忽略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',

)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。