本文整理汇总了Python中django.template.context.RequestContext方法的典型用法代码示例。如果您正苦于以下问题:Python context.RequestContext方法的具体用法?Python context.RequestContext怎么用?Python context.RequestContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.context
的用法示例。
在下文中一共展示了context.RequestContext方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_csv
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def export_csv(request, sheet, csv_format):
if csv_format == 'unicode':
encoding = 'utf-8'
else:
encoding = 'windows-1252'
try:
content = worksheet_to_csv(
sheet.unjsonify_worksheet(),
encoding=encoding
)
except UnicodeEncodeError:
return render(
request,
'export_csv_error.html',
{'sheet': sheet},
context_instance=RequestContext(request)
)
response = HttpResponse(content, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s.csv' % (sheet.name,)
response['Content-Length'] = len(content)
return response
示例2: get_context_dict
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def get_context_dict(context):
"""
Contexts in django version 1.9+ must be dictionaries. As xadmin has a legacy with older versions of django,
the function helps the transition by converting the [RequestContext] object to the dictionary when necessary.
:param context: RequestContext
:return: dict
"""
if isinstance(context, RequestContext):
ctx = context.flatten()
else:
ctx = context
return ctx
示例3: render
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def render(self, context=None, request=None):
# A deprecation path is required here to cover the following usage:
# >>> from django.template import Context
# >>> from django.template.loader import get_template
# >>> template = get_template('hello.html')
# >>> template.render(Context({'name': 'world'}))
# In Django 1.7 get_template() returned a django.template.Template.
# In Django 1.8 it returns a django.template.backends.django.Template.
# In Django 1.10 the isinstance checks should be removed. If passing a
# Context or a RequestContext works by accident, it won't be an issue
# per se, but it won't be officially supported either.
if isinstance(context, RequestContext):
if request is not None and request is not context.request:
raise ValueError(
"render() was called with a RequestContext and a request "
"argument which refer to different requests. Make sure "
"that the context argument is a dict or at least that "
"the two arguments refer to the same request.")
warnings.warn(
"render() must be called with a dict, not a RequestContext.",
RemovedInDjango110Warning, stacklevel=2)
elif isinstance(context, Context):
warnings.warn(
"render() must be called with a dict, not a Context.",
RemovedInDjango110Warning, stacklevel=2)
else:
context = make_context(context, request)
return self.template.render(context)
示例4: import_xls
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def import_xls(request, username):
if request.user.username != username:
return HttpResponseForbidden(render_to_string("403.html"))
handle, filename = mkstemp()
try:
os.write(handle, request.FILES['file'].read())
wb = xlrd.open_workbook(filename)
for xl_sheet in wb.sheets():
if xl_sheet.nrows > 0 and xl_sheet.ncols > 0:
name = '%s - %s' % (
splitext(request.FILES['file'].name)[0],
xl_sheet.name
)
sheet = Sheet(owner=request.user, name=name)
sheet.jsonify_worksheet(worksheet_from_excel(xl_sheet))
sheet.save()
try:
calculate(request, sheet.owner.username, sheet.id)
except:
pass
except Exception:
return render(
request,
'import_xls_error.html',
{},
context_instance=RequestContext(request)
)
finally:
os.close(handle)
os.unlink(filename)
return HttpResponseRedirect('/')
示例5: import_csv
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def import_csv(request, sheet):
def error_response():
return render(
request,
'import_csv_error.html',
{'sheet': sheet},
context_instance=RequestContext(request)
)
form = ImportCSVForm(request.POST, request.FILES)
if not form.is_valid():
return error_response()
worksheet = sheet.unjsonify_worksheet()
csv_file = request.FILES['file']
column = form.cleaned_data['column']
row = form.cleaned_data['row']
excel_file_encoding = request.POST['csv_encoding']=='excel'
try:
worksheet = worksheet_from_csv(
worksheet, csv_file, column, row, excel_file_encoding
)
except DirigibleImportError:
return error_response()
sheet.jsonify_worksheet(worksheet)
if update_sheet_with_version_check(sheet, contents_json=sheet.contents_json):
calculate(request, sheet.owner.username, sheet.id)
return HttpResponseRedirect(reverse('sheet_page',kwargs={
'username' : request.user.username,
'sheet_id' : sheet.id,
}))
else:
return HttpResponse('FAILED')
示例6: render
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def render(self, context=None, request=None):
# A deprecation path is required here to cover the following usage:
# >>> from django.template import Context
# >>> from django.template.loader import get_template
# >>> template = get_template('hello.html')
# >>> template.render(Context({'name': 'world'}))
# In Django 1.7 get_template() returned a django.template.Template.
# In Django 1.8 it returns a django.template.backends.django.Template.
# In Django 1.10 the isinstance checks should be removed. If passing a
# Context or a RequestContext works by accident, it won't be an issue
# per se, but it won't be officially supported either.
if isinstance(context, RequestContext):
if request is not None and request is not context.request:
raise ValueError(
"render() was called with a RequestContext and a request "
"argument which refer to different requests. Make sure "
"that the context argument is a dict or at least that "
"the two arguments refer to the same request.")
warnings.warn(
"render() must be called with a dict, not a RequestContext.",
RemovedInDjango110Warning, stacklevel=2)
elif isinstance(context, Context):
warnings.warn(
"render() must be called with a dict, not a Context.",
RemovedInDjango110Warning, stacklevel=2)
else:
context = make_context(context, request)
try:
return self.template.render(context)
except TemplateDoesNotExist as exc:
reraise(exc, self.backend)
示例7: widget
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def widget(self):
context = {'widget_id': self.id, 'widget_title': self.title, 'widget_icon': self.widget_icon,
'widget_type': self.widget_type, 'form': self, 'widget': self}
self.context(context)
return loader.render_to_string(self.template, context, context_instance=RequestContext(self.request))
示例8: get_context_dict
# 需要导入模块: from django.template import context [as 别名]
# 或者: from django.template.context import RequestContext [as 别名]
def get_context_dict(context):
"""
Contexts in django version 1.9+ must be dictionaries. As xadmin has a legacy with older versions of django,
the function helps the transition by converting the [RequestContext] object to the dictionary when necessary.
:param context: RequestContext
:return: dict
"""
if isinstance(context, RequestContext):
ctx = {}
map(ctx.update, context.dicts)
else:
ctx = context
return ctx