本文整理汇总了Python中django.http.response.HttpResponseBase方法的典型用法代码示例。如果您正苦于以下问题:Python response.HttpResponseBase方法的具体用法?Python response.HttpResponseBase怎么用?Python response.HttpResponseBase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.http.response
的用法示例。
在下文中一共展示了response.HttpResponseBase方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_response
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import HttpResponseBase [as 别名]
def write_response(self, response):
if isinstance(response, RedirectResponse):
result = HttpResponseRedirect(response.url, status=response.status)
elif isinstance(response, OptionalJSONResponse) and isinstance(response.data, HttpResponseBase):
result = response.data
elif isinstance(response, TemplateResponse):
template_path = os.path.join(base_settings.BASE_DIR, 'templates', response.template)
with open(template_path, 'r') as file:
template = file.read()
template = template.replace('{% end %}', '{% endif %}')
context = Context(response.data)
content = Template(template).render(context)
result = HttpResponse(content, status=response.status)
else:
result = HttpResponse(response.render(), status=response.status)
for name, value in self.view.default_headers().items():
result[name] = value
for name, value in response.header_items():
result[name] = value
return result
示例2: finalize_response
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import HttpResponseBase [as 别名]
def finalize_response(self, request, response, *args, **kwargs):
"""
Returns the final response object.
"""
# Make the error obvious if a proper response is not returned
assert isinstance(response, HttpResponseBase), (
'Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` '
'to be returned from the view, but received a `%s`'
% type(response)
)
if isinstance(response, Response):
if not getattr(request, 'accepted_renderer', None):
neg = self.perform_content_negotiation(request, force=True)
request.accepted_renderer, request.accepted_media_type = neg
response.accepted_renderer = request.accepted_renderer
response.accepted_media_type = request.accepted_media_type
response.renderer_context = self.get_renderer_context()
for key, value in self.headers.items():
response[key] = value
return response
示例3: rollback
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import HttpResponseBase [as 别名]
def rollback(using=None, sid=None):
"""
Possibility of calling transaction.rollback() in new Django versions (in atomic block).
Important: transaction savepoint (sid) is required for Django < 1.8
"""
if sid:
django.db.transaction.savepoint_rollback(sid)
else:
try:
django.db.transaction.rollback(using)
except django.db.transaction.TransactionManagementError:
django.db.transaction.set_rollback(True, using)
# HttpResponseBase only exists from 1.5 onwards
示例4: finalize_response
# 需要导入模块: from django.http import response [as 别名]
# 或者: from django.http.response import HttpResponseBase [as 别名]
def finalize_response(self, request, response, *args, **kwargs):
"""
Returns the final response object.
"""
# Make the error obvious if a proper response is not returned
assert isinstance(response, HttpResponseBase), (
'Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` '
'to be returned from the view, but received a `%s`'
% type(response)
)
if isinstance(response, Response):
if not getattr(request, 'accepted_renderer', None):
neg = self.perform_content_negotiation(request, force=True)
request.accepted_renderer, request.accepted_media_type = neg
response.accepted_renderer = request.accepted_renderer
response.accepted_media_type = request.accepted_media_type
response.renderer_context = self.get_renderer_context()
# Add new vary headers to the response instead of overwriting.
vary_headers = self.headers.pop('Vary', None)
if vary_headers is not None:
patch_vary_headers(response, cc_delim_re.split(vary_headers))
for key, value in self.headers.items():
response[key] = value
return response