本文整理汇总了Python中django.template.context.Context类的典型用法代码示例。如果您正苦于以下问题:Python Context类的具体用法?Python Context怎么用?Python Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_cell
def render_cell(self, state, obj, render_context):
"""Renders the table cell containing column data."""
datagrid = state.datagrid
rendered_data = self.render_data(state, obj)
url = ''
css_class = ''
if self.link:
try:
url = self.link_func(state, obj, rendered_data)
except AttributeError:
pass
if self.css_class:
if six.callable(self.css_class):
css_class = self.css_class(obj)
else:
css_class = self.css_class
key = "%s:%s:%s:%s" % (state.last, rendered_data, url, css_class)
if key not in state.cell_render_cache:
ctx = Context(render_context)
ctx.update({
'column': self,
'column_state': state,
'css_class': css_class,
'url': url,
'data': mark_safe(rendered_data)
})
state.cell_render_cache[key] = \
mark_safe(datagrid.cell_template_obj.render(ctx))
return state.cell_render_cache[key]
示例2: submit_row
def submit_row(context):
"""
移除"保存后添加"按钮
移除"保存继续编辑"按钮
添加"保存为草稿"按钮
"""
# ctx = original_submit_row(context)
change = context['change']
is_popup = context['is_popup']
save_as = context['save_as']
show_save = context.get('show_save', True)
show_save_and_continue = context.get('show_save_and_continue', True)
from django.template.context import Context
ctx = Context(context)
ctx.update({
'show_delete_link': True,
'show_save_as_new': not is_popup and change and save_as,
'show_save_and_add_another': (
context['has_add_permission'] and not is_popup and
(not save_as or context['add'])
),
'show_save_and_continue': not is_popup and context['has_change_permission'] and show_save_and_continue,
'show_save': False,
'show_save_as_draft': True,
})
# ctx.update({
# 'show_save_and_add_another': False,
# 'show_save_as_draft': True,
# 'show_save_and_continue': False,
# })
print(ctx)
return ctx
示例3: render
def render(self, context, instance, placeholder):
template_vars = {
'placeholder': placeholder,
}
template_vars['object'] = instance
# locate the plugins assigned to the given page for the indicated placeholder
lang = None
if context.has_key('request'):
lang = context['request'].LANGUAGE_CODE
else:
lang = settings.LANGUAGE_CODE
#print 'language CONTEXT FOR PLUGIN:', lang
plugins = CMSPlugin.objects.filter(page=instance.parent_page, placeholder=placeholder, language=lang)
plugin_output = []
template_vars['parent_plugins'] = plugins
for plg in plugins:
#print 'added a parent plugin:', plg, plg.__class__
# use a temporary context to prevent plugins from overwriting context
tmpctx = Context()
tmpctx.update(template_vars)
inst, name = plg.get_plugin_instance()
#print 'got a plugin instance:', inst
outstr = inst.render_plugin(tmpctx, placeholder)
plugin_output.append(outstr)
#print 'render result:', outstr
template_vars['parent_output'] = plugin_output
context.update(template_vars)
return context
示例4: render_globals
def render_globals(self):
print "!!"
result = ""
if self.template_globals:
self.templates_list.insert(0, self.template_globals)
default_dict = {"component": self, "window": self}
context = Context(default_dict)
for file_name in self.templates_list:
if isinstance(file_name, (tuple, list)):
file_context = Context(default_dict)
file_context.update(file_name[1])
file_name = file_name[0]
else:
file_context = context
template = get_template(file_name)
try:
text = template.render(file_context)
except Exception as err:
# не надо молчать если есть проблемы
raise ApplicationLogicException(
"Render error for template {} {}".format(file_name, get_exception_description(err))
)
# Комментарий для отладки
remark = "\n//========== TEMPLATE: %s ==========\n" % file_name
result += remark + text
# Отмечает строку как безопасную (фильтр safe) для шаблонизатора
return mark_safe(result)
示例5: submit_row
def submit_row(context):
"""
Display the row of buttons for delete and save.
"""
add = context['add']
change = context['change']
is_popup = context['is_popup']
save_as = context['save_as']
show_save = context.get('show_save', True)
show_save_and_continue = context.get('show_save_and_continue', True)
has_add_permission = context['has_add_permission']
has_change_permission = context['has_change_permission']
has_view_permission = context['has_view_permission']
has_editable_inline_admin_formsets = context['has_editable_inline_admin_formsets']
can_save = (has_change_permission and change) or (has_add_permission and add) or has_editable_inline_admin_formsets
can_save_and_continue = not is_popup and can_save and has_view_permission and show_save_and_continue
can_change = has_change_permission or has_editable_inline_admin_formsets
ctx = Context(context)
ctx.update({
'can_change': can_change,
'show_delete_link': (
not is_popup and context['has_delete_permission'] and
change and context.get('show_delete', True)
),
'show_save_as_new': not is_popup and has_change_permission and change and save_as,
'show_save_and_add_another': (
has_add_permission and not is_popup and
(not save_as or add) and can_save
),
'show_save_and_continue': can_save_and_continue,
'show_save': show_save and can_save,
'show_close': not(show_save and can_save)
})
return ctx
示例6: sphblog_showblogpost
def sphblog_showblogpost(context, post):
ret = {'post': post,
'blogpost': post.blogpostextension_set.get(),
}
retctx = Context(context)
retctx.update(ret)
return retctx
示例7: show_post
def show_post(context, post):
''' shows an actual blog post '''
ctx = {'post': post,
'published_comment_count': post.published_comment_count()}
ret = Context(context)
ret.update(ctx)
return ret
示例8: render
def render(self, loader):
"""
Function - create html structure of folder's tree
Parameters
==========
loader - django.template.loader object
"""
if loader is django.template.loader:
ctx = Context()
t = loader.get_template('tinymce_images_foldertree.html')
if self.is_top():
ctx['is_top'] = 1
else:
ctx['relpath'] = self.path()
ctx['path'] = self._path
ctx['files_count'] = self.get_files_count()
ctx['folders_count'] = self.get_folders_count()
# for each item calls render function
for item in self._items:
ctx['inner'] = ctx.get('inner', '') + item.render(loader)
return t.render(ctx)
示例9: test_sitecats_url
def test_sitecats_url(self):
tpl = '{% load sitecats %}{% sitecats_url %}'
self.assertRaises(TemplateSyntaxError, render_string, tpl)
# testing UNRESOLVED_URL_MARKER
context = Context({'my_category': self.cat1, 'my_list': self.cl_cat1})
tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}'
result = render_string(tpl, context)
self.assertEqual(result, UNRESOLVED_URL_MARKER)
# testing ModelWithCategory.get_category_absolute_url()
self.cl_cat1.set_obj(self.art1)
expected_url = '%s/%s' % (self.cat1.id, self.art1.title)
tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}'
result = render_string(tpl, context)
self.assertEqual(result, expected_url)
tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list as someurl %}'
render_string(tpl, context)
self.assertEqual(context.get('someurl'), expected_url)
# testing CategoryList.show_links
tpl = '{% load sitecats %}{% sitecats_url for my_category using my_list %}'
result = render_string(tpl, Context({'my_category': self.cat2, 'my_list': self.cl_cat2}))
self.assertEqual(result, str(self.cat2.id))
示例10: get_context
def get_context(context_dict=None, current_app='', request_path=None, user_data=None):
user = user_data if hasattr(user_data, '_meta') else MockUser(user_data)
context_dict = context_dict or {}
context_updater = {
'request': MockRequest(request_path, user),
}
if user_data:
context_updater['user'] = user
context_dict.update(context_updater)
context = Context(context_dict)
context.template = mock.MagicMock()
context.template.engine.string_if_invalid = ''
if VERSION >= (1, 10):
match = mock.MagicMock()
match.app_name = current_app
context.resolver_match = match
else:
context._current_app = current_app
return context
示例11: __init__
def __init__(self, dict, instance, placeholder, processors=None, current_app=None):
Context.__init__(self, dict, current_app=current_app)
if processors is None:
processors = ()
else:
processors = tuple(processors)
for processor in DEFAULT_PLUGIN_CONTEXT_PROCESSORS + get_standard_processors('CMS_PLUGIN_CONTEXT_PROCESSORS') + processors:
self.update(processor(instance, placeholder))
示例12: render
def render(self, context):
form = FriendsSearchForm()
ctx = Context()
[ ctx.update(d) for d in context.dicts ]
ctx.update({
'friends_search_form' : form,
})
return render_to_string('friends/tags/search_form{0}.html'.format(self.postfix), ctx)
示例13: sphboard_displayCategories
def sphboard_displayCategories(context, categories, maxDepth=5, level=-1):
if maxDepth < level:
return { }
ret = {'categories': [c for c in categories if c.get_category_type().is_displayed()],
'level': level + 1,
'maxDepth': maxDepth}
retctx = Context(context)
retctx.update(ret)
return retctx
示例14: render
def render(self, **kwargs):
assert 'request' in kwargs
template = getattr(self, 'render_template', getattr(self.get_content(), 'render_template', None) if hasattr(self, 'get_content') else None)
if not template:
raise NotImplementedError('No template defined for rendering %s content.' % self.__class__.__name__)
context = Context({'content': self})
if 'context' in kwargs:
context.update(kwargs['context'])
return render_to_string(template, context, context_instance=RequestContext(kwargs['request']))
示例15: __init__
def __init__(self, dict=None, processors=None, current_app=None):
Context.__init__(self, dict, current_app=current_app)
if processors is None:
processors = ()
else:
processors = tuple(processors)
for processor in (
tuple(p for p in get_standard_processors() if getattr(p, "requires_request", True) == False) + processors
):
self.update(processor())