本文整理汇总了Python中django.test.client.CONTENT_TYPE_RE.match方法的典型用法代码示例。如果您正苦于以下问题:Python CONTENT_TYPE_RE.match方法的具体用法?Python CONTENT_TYPE_RE.match怎么用?Python CONTENT_TYPE_RE.match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.test.client.CONTENT_TYPE_RE
的用法示例。
在下文中一共展示了CONTENT_TYPE_RE.match方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def post(self, path, data={}, content_type=MULTIPART_CONTENT,
**extra):
"Construct a POST request."
if content_type is MULTIPART_CONTENT:
post_data = encode_multipart(BOUNDARY, data)
else:
# Encode the content so that the byte representation is correct.
match = CONTENT_TYPE_RE.match(content_type)
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
post_data = smart_str(data, encoding=charset)
parsed = urlparse(path)
r = {
'CONTENT_LENGTH': len(post_data),
'CONTENT_TYPE': content_type,
'PATH_INFO': self._get_path(parsed),
'QUERY_STRING': parsed[4],
'REQUEST_METHOD': 'POST',
'wsgi.input': FakePayload(post_data),
}
r.update(extra)
return self.request(**r)
示例2: put
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def put(self, path, data={}, content_type=MULTIPART_CONTENT,
follow=False, **extra):
"""
Requests a response from the server using POST.
"""
if content_type is MULTIPART_CONTENT:
post_data = encode_multipart(BOUNDARY, data)
else:
# Encode the content so that the byte representation is correct.
match = CONTENT_TYPE_RE.match(content_type)
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
post_data = smart_str(data, encoding=charset)
parsed = urlparse(path)
r = {
'CONTENT_LENGTH': len(post_data),
'CONTENT_TYPE': content_type,
'PATH_INFO': urllib.unquote(parsed[2]),
'QUERY_STRING': parsed[4],
'REQUEST_METHOD': 'PUT',
'wsgi.input': FakePayload(post_data),
}
r.update(extra)
response = self.request(**r)
if follow:
response = self._handle_redirects(response)
return response
示例3: return_text_file
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def return_text_file(request):
"A view that parses and returns text as a file."
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
return HttpResponse(request.body, status=200, content_type='text/plain; charset=%s' % charset)
示例4: _encode_data
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def _encode_data(self, data, content_type, ):
if content_type is MULTIPART_CONTENT:
return encode_multipart(BOUNDARY, data)
else:
# Encode the content so that the byte representation is correct.
match = CONTENT_TYPE_RE.match(content_type)
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
return smart_str(data, encoding=charset)
示例5: return_json_file
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def return_json_file(request):
"A view that parses and returns a JSON string as a file."
match = CONTENT_TYPE_RE.match(request.META["CONTENT_TYPE"])
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
# This just checks that the uploaded data is JSON
obj_dict = json.loads(request.body.decode(charset))
obj_json = json.dumps(obj_dict, cls=DjangoJSONEncoder, ensure_ascii=False)
response = HttpResponse(obj_json.encode(charset), status=200, content_type="application/json; charset=%s" % charset)
response["Content-Disposition"] = "attachment; filename=testfile.json"
return response
示例6: return_json_file
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def return_json_file(request, default_charset='utf-8'):
"A view that parses and returns a JSON string as a file."
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
if match:
charset = match.group(1)
else:
charset = default_charset
# This just checks that the uploaded data is JSON
obj_dict = json.loads(request.body.decode(charset))
obj_json = json.dumps(obj_dict, cls=DjangoJSONEncoder, ensure_ascii=False)
response = HttpResponse(obj_json.encode(charset), status=200,
content_type='application/json; charset=%s' % charset)
response['Content-Disposition'] = 'attachment; filename=testfile.json'
return response
示例7: return_json_file
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def return_json_file(request):
"A view that parses and returns a JSON string as a file."
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
# This just checks that the uploaded data is JSON
obj_dict = simplejson.loads(request.raw_post_data.decode(charset))
obj_json = simplejson.dumps(obj_dict, encoding=charset,
cls=DjangoJSONEncoder,
ensure_ascii=False)
response = HttpResponse(smart_str(obj_json, encoding=charset), status=200,
mimetype='application/json; charset=' + charset)
response['Content-Disposition'] = 'attachment; filename=testfile.json'
return response
示例8: endpoint
# 需要导入模块: from django.test.client import CONTENT_TYPE_RE [as 别名]
# 或者: from django.test.client.CONTENT_TYPE_RE import match [as 别名]
def endpoint(request):
if request.method != 'POST':
return HttpResponseBadRequest()
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
if match:
charset = match.group(1)
else:
charset = settings.DEFAULT_CHARSET
data = json.loads(request.body.decode(charset))
run_actions(request, data)
result = {}
for (namespace, render) in achilles_renders().items():
result[namespace] = render(request)
return HttpResponse(json.dumps(result), content_type="application/json")