本文整理汇总了Python中django.template.VariableDoesNotExist方法的典型用法代码示例。如果您正苦于以下问题:Python template.VariableDoesNotExist方法的具体用法?Python template.VariableDoesNotExist怎么用?Python template.VariableDoesNotExist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template
的用法示例。
在下文中一共展示了template.VariableDoesNotExist方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
if '.' in self.field_var:
base, field_name = self.field_var.rsplit('.', 1)
field = getattr(Variable(base).resolve(context), field_name)
else:
field = context[self.field_var]
except (template.VariableDoesNotExist, KeyError, AttributeError):
return settings.TEMPLATE_STRING_IF_INVALID
h_attrs = {}
for k, v in iteritems(self.html_attrs):
try:
h_attrs[k] = v.resolve(context)
except template.VariableDoesNotExist:
h_attrs[k] = settings.TEMPLATE_STRING_IF_INVALID
return field(**h_attrs)
示例2: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
value = self.block_var.resolve(context)
except template.VariableDoesNotExist:
return ''
if hasattr(value, 'render_as_block'):
if self.use_parent_context:
new_context = context.flatten()
else:
new_context = {}
if self.extra_context:
for var_name, var_value in self.extra_context.items():
new_context[var_name] = var_value.resolve(context)
return value.render_as_block(context=new_context)
else:
return force_str(value)
示例3: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
value = self.block_var.resolve(context)
except template.VariableDoesNotExist:
return ''
if isinstance(value, dict): # its an preview
code_renderer = CodeRenderer()
bl_class = get_block_class_for(value.get('type', 'INVALID_PLUGIN_WITH_NO_TYPE'))
if bl_class:
instance = bl_class(value, code_renderer)
return instance.render(context)
else:
return ''
elif isinstance(value, str):
try:
value = HyperFieldResponse.from_str(value)
return value.render(context)
except Exception as e:
print(e)
return ''
else:
return value.render(context)
示例4: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
expire_time = self.expire_time_var.resolve(context)
except VariableDoesNotExist:
raise TemplateSyntaxError('"cache" tag got an unknown variable: %r' % self.expire_time_var.var)
try:
expire_time = int(expire_time)
except (ValueError, TypeError):
raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time)
# Build a key for this fragment and all vary-on's.
key = ':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on])
args = hashlib.md5(force_bytes(key))
cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())
value = cache.get(cache_key)
if value is None:
value = self.nodelist.render(context)
cache.set(cache_key, value, expire_time)
return value
示例5: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
user = self.user.resolve(context)
except template.VariableDoesNotExist:
return ""
if not self.um_to_user:
message_count = MessageRecipient.objects.count_unread_messages_for(user)
else:
try:
um_to_user = self.um_to_user.resolve(context)
except template.VariableDoesNotExist:
return ""
message_count = MessageRecipient.objects.count_unread_messages_between(
user, um_to_user
)
context[self.var_name] = message_count
return ""
示例6: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
if "." in self.manager_method:
manager, method = self.manager_method.split(".")
else:
manager = "_default_manager"
method = self.manager_method
model_manager = getattr(self.model, manager)
fallback_method = self.model._default_manager.none
qs = getattr(model_manager, method, fallback_method)()
limit = None
if self.limit:
try:
limit = self.limit.resolve(context)
except template.VariableDoesNotExist:
limit = None
context[self.var_name] = qs[:limit] if limit else qs
return ""
开发者ID:PacktPublishing,项目名称:Django-2-Web-Development-Cookbook-Third-Edition,代码行数:20,代码来源:utility_tags.py
示例7: test_log_on_variable_does_not_exist_not_silent
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def test_log_on_variable_does_not_exist_not_silent(self):
with self.assertLogs('django.template', self.loglevel) as cm:
with self.assertRaises(VariableDoesNotExist):
Variable('article.author').resolve({'article': {'section': 'News'}})
self.assertEqual(len(cm.records), 1)
log_record = cm.records[0]
self.assertEqual(
log_record.getMessage(),
"Exception while resolving variable 'author' in template 'unknown'."
)
self.assertIsNotNone(log_record.exc_info)
raised_exception = log_record.exc_info[1]
self.assertEqual(
str(raised_exception),
"Failed lookup for key [author] in {'section': 'News'}"
)
示例8: style_attribute
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def style_attribute(attribute_name, package):
mappings = {
'title': style_title,
'repo_description': style_repo_description,
'commits_over_52': style_commits,
}
as_var = template.Variable('package.' + attribute_name)
try:
value = as_var.resolve({'package': package})
except template.VariableDoesNotExist:
value = ''
if attribute_name in list(mappings.keys()):
return mappings[attribute_name](value)
return style_default(value)
示例9: takes_current_page
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def takes_current_page(func):
"""Decorator for template tags that require the current page."""
@wraps(func)
def do_takes_current_page(context, *args, **kwargs):
page = get_page_from_context(context, kwargs)
if not page:
page = get_default_news_page()
if page is None:
raise template.VariableDoesNotExist("Could not determine the current page from the template context.")
kwargs["page"] = page
return func(context, *args, **kwargs)
return do_takes_current_page
示例10: tryresolve
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def tryresolve(var, context, default=None):
try:
return var.resolve(context)
except template.VariableDoesNotExist:
return default
示例11: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
time = self.time.resolve(context)
try:
now = datetime.datetime.now() - time
except TypeError:
return ''
return '%d.%d seconds' % (now.seconds, now.microseconds)
except template.VariableDoesNotExist:
return ''
示例12: hash
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def hash(object, attr):
pseudo_context = { 'object' : object }
try:
value = Variable('object.%s' % attr).resolve(pseudo_context)
except VariableDoesNotExist:
value = None
return value
示例13: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
dictionary = self.dictionary.resolve(context)
aslug = self.aslug.resolve(context)
userid = self.userid.resolve(context)
except template.VariableDoesNotExist:
return "?"
grades = dictionary[aslug]
if userid not in grades:
return ''
grade = grades[userid]
gtext = escape(grade.display_staff_short())
stext = ''
if grade.flag not in ['GRAD', 'NOGR', 'CALC']:
stext = '(' + grade.get_flag_display() + ')'
if grade.comment:
ctext = escape(grade.comment)
stext = '(' + grade.get_flag_display() + ')'
else:
ctext = ''
if ctext and stext:
return mark_safe(gtext + '<span class="more"><span title="' + ctext + '"><img src="/media/icons/information.png" alt="[I]" /></span><br/>' + stext + '</span>')
elif stext:
return mark_safe(gtext + '<span class="more"><br/>' + stext + '</span>')
else:
return mark_safe(gtext)
示例14: render
# 需要导入模块: from django import template [as 别名]
# 或者: from django.template import VariableDoesNotExist [as 别名]
def render(self, context):
try:
self.value = self.value.resolve(context)
if not isinstance(self.value, ImageFieldFile):
raise template.VariableDoesNotExist(
'Image argument should be an ImageField')
images = context.dicts[0].setdefault('ootemplate_imgs', {})
id = len(images)
z_index = id + 3 # Magic
width = self.value.width * PIXEL_TO_CM
height = self.value.height * PIXEL_TO_CM
filename = os.path.basename(self.value.name)
basename = os.path.splitext(filename)[0]
images[self.value.path] = self.value
return ('<draw:frame draw:style-name="gr%(z_index)s" '
'draw:name="%(basename)s" '
'draw:id="id%(id)s" '
'text:anchor-type="char" svg:width="%(width)fcm" '
'svg:height="%(height)fcm" draw:z-index="%(z_index)s">'
'<draw:image xlink:href="Pictures/%(filename)s" '
'xlink:type="simple" xlink:show="embed" '
'xlink:actuate="onLoad"/></draw:frame>') % locals()
except template.VariableDoesNotExist:
return ''