本文整理匯總了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")