我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用django.template.VariableDoesNotExist()。
def handle_var(value, context): """ Handle template tag variable """ # Resolve FilterExpression and Variable immediately if isinstance(value, FilterExpression) or isinstance(value, Variable): return value.resolve(context) # Return quoted strings unquoted # http://djangosnippets.org/snippets/886 stringval = QUOTED_STRING.search(value) if stringval: return stringval.group('noquotes') # Resolve variable or return string value try: return Variable(value).resolve(context) except VariableDoesNotExist: return value
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)
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)
def render(self, context): try: image = self.image_expr.resolve(context) except template.VariableDoesNotExist: return '' if not image: return '' rendition = get_rendition_or_not_found(image, self.filter) if self.output_var_name: # return the rendition object in the given variable context[self.output_var_name] = rendition return '' else: # render the rendition's image tag now resolved_attrs = {} for key in self.attrs: resolved_attrs[key] = self.attrs[key].resolve(context) return rendition.img_tag(resolved_attrs)
def render(self, context): try: value_missing = False value = self.variable.resolve(context, True) except VariableDoesNotExist: no_value = True value_missing = None for tests, nodelist in self.cases: if tests is None: return nodelist.render(context) elif not value_missing: for test in tests: test_value = test.resolve(context, True) if value == test_value: return nodelist.render(context) else: return ""
def get_current_app(request): try: return request.current_app except (VariableDoesNotExist, AttributeError): pass return None
def render(self, context): try: email = self.email.resolve(context) except template.VariableDoesNotExist: return '' # default = 'http://example.com/static/images/defaultavatar.jpg' default = 'mm' # Mystery Man size = 24 return '//www.gravatar.com/avatar/{}?{}'.format( hashlib.md5(email.lower()).hexdigest(), urllib.urlencode({'d': default, 's': str(size)}))
def render(self, context): try: project = self.project.resolve(context) if project.is_default: return reverse('projectbuilds', args=(project.id,)) else: return reverse('project', args=(project.id,)) except template.VariableDoesNotExist: return ''
def render(self, context): try: actual_total = self.total.resolve(context) actual_used = self.used.resolve(context) except template.VariableDoesNotExist: return '' return 100 / (actual_total / actual_used)
def render(self, context): try: actual_total = self.total.resolve(context) actual_used = self.used.resolve(context) except template.VariableDoesNotExist: return '' pc = (100 / (actual_total / actual_used)) if pc < 50: return 'success' if pc < 75: return 'warning' return 'danger'
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 ''
def render(self, context): try: email = self.email.resolve(context) except template.VariableDoesNotExist: return '' default = "http://example.com/static/images/defaultavatar.jpg" size = 40 gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?" gravatar_url += urllib.urlencode({'d':default, 's':str(size)}) return gravatar_url
def get_target_ctype_pk(self, context): if self.object_expr: try: obj = self.object_expr.resolve(context) except template.VariableDoesNotExist: return None, None return ContentType.objects.get_for_model(obj), obj.pk else: return self.ctype, self.object_pk_expr.resolve(context, ignore_failures=True)
def get_object(self, context): if self.object_expr: try: return self.object_expr.resolve(context) except template.VariableDoesNotExist: return None else: object_pk = self.object_pk_expr.resolve(context, ignore_failures=True) return self.ctype.get_object_for_this_type(pk=object_pk)
def render(self, context): try: resolved_var = template.resolve_variable( self.var_to_resolve, context ) self.get_context(context)[self.var_name] = resolved_var except template.VariableDoesNotExist: self.get_context(context)[self.var_name] = "" return ""
def render(self, context): request = context.get('request') try: v = template.Variable(self.title) self.title = v.resolve(context) except template.VariableDoesNotExist: self.title = self.title.strip("'").strip('"') if self._check_perm(request.user, force_lang=self.lang, current_lang=request.LANGUAGE_CODE): return self.nodelist_true.render(context) else: return self.nodelist_false.render(context)
def render(self, context): # Nodes are not threadsafe so we must store and look up our instance # variables in the current rendering context first if self not in context.render_context: context.render_context[self] = ( template.Variable(self.field), self.attrs, template.Variable(self.html5_required) ) field, attrs, html5_required = context.render_context[self] field = field.resolve(context) try: html5_required = html5_required.resolve(context) except template.VariableDoesNotExist: html5_required = False widgets = getattr(field.field.widget, 'widgets', [field.field.widget]) if isinstance(attrs, dict): attrs = [attrs] * len(widgets) for widget, attr in zip(widgets, attrs): class_name = widget.__class__.__name__.lower() class_name = class_converter.get(class_name, class_name) css_class = widget.attrs.get('class', '') if css_class: if css_class.find(class_name) == -1: css_class += " %s" % class_name else: css_class = class_name if ( TEMPLATE_PACK == 'bootstrap3' and not is_checkbox(field) and not is_file(field) ): css_class += ' form-control' widget.attrs['class'] = css_class # HTML5 required attribute if html5_required and field.field.required and 'required' not in widget.attrs: if field.field.widget.__class__.__name__ is not 'RadioSelect': widget.attrs['required'] = 'required' for attribute_name, attribute in attr.items(): attribute_name = template.Variable(attribute_name).resolve(context) if attribute_name in widget.attrs: widget.attrs[attribute_name] += " " + template.Variable(attribute).resolve(context) else: widget.attrs[attribute_name] = template.Variable(attribute).resolve(context) return field