我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用django.conf.settings.TEMPLATE_STRING_IF_INVALID。
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 get_attribute(value, arg, return_as_string=True): """ Gets an attribute of an object dynamically from a string name """ numeric_test = re.compile("^\d+$") if hasattr(value, '__iter__') and arg in value: value = value[arg] elif hasattr(value, str(arg)): value = getattr(value, arg) elif numeric_test.match(str(arg)) and len(value) > int(arg): value = value[int(arg)] else: return settings.TEMPLATE_STRING_IF_INVALID if hasattr(value, '__call__'): value = value() if return_as_string: value = mark_safe(value) return value
def lookup(dictionary, key): """ Utility template filter, as Django forbids argument passing in templates. Used for filtering out values, e.g. for metadata, of list has EDDObject items and type is a MetadataType: {%% for obj in list %%} {{ obj.metadata|lookup:type }} {%% endfor %%} """ return dictionary.get(key, settings.TEMPLATE_STRING_IF_INVALID)
def templates(self): if self._templates is None: self._templates = settings.TEMPLATES if not self._templates: warnings.warn( "You haven't defined a TEMPLATES setting. You must do so " "before upgrading to Django 1.10. Otherwise Django will be " "unable to load templates.", RemovedInDjango110Warning) self._templates = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': settings.TEMPLATE_DIRS, 'OPTIONS': { 'allowed_include_roots': settings.ALLOWED_INCLUDE_ROOTS, 'context_processors': settings.TEMPLATE_CONTEXT_PROCESSORS, 'debug': settings.TEMPLATE_DEBUG, 'loaders': settings.TEMPLATE_LOADERS, 'string_if_invalid': settings.TEMPLATE_STRING_IF_INVALID, }, }, ] templates = OrderedDict() backend_names = [] for tpl in self._templates: tpl = tpl.copy() try: # This will raise an exception if 'BACKEND' doesn't exist or # isn't a string containing at least one dot. default_name = tpl['BACKEND'].rsplit('.', 2)[-2] except Exception: invalid_backend = tpl.get('BACKEND', '<not defined>') raise ImproperlyConfigured( "Invalid BACKEND for a template engine: {}. Check " "your TEMPLATES setting.".format(invalid_backend)) tpl.setdefault('NAME', default_name) tpl.setdefault('DIRS', []) tpl.setdefault('APP_DIRS', False) tpl.setdefault('OPTIONS', {}) templates[tpl['NAME']] = tpl backend_names.append(tpl['NAME']) counts = Counter(backend_names) duplicates = [alias for alias, count in counts.most_common() if count > 1] if duplicates: raise ImproperlyConfigured( "Template engine aliases aren't unique, duplicates: {}. " "Set a unique NAME for each engine in settings.TEMPLATES." .format(", ".join(duplicates))) return templates
def templates(self): if self._templates is None: self._templates = settings.TEMPLATES if not self._templates: warnings.warn( "You haven't defined a TEMPLATES setting. You must do so " "before upgrading to Django 2.0. Otherwise Django will be " "unable to load templates.", RemovedInDjango20Warning) self._templates = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': settings.TEMPLATE_DIRS, 'OPTIONS': { 'allowed_include_roots': settings.ALLOWED_INCLUDE_ROOTS, 'context_processors': settings.TEMPLATE_CONTEXT_PROCESSORS, 'debug': settings.TEMPLATE_DEBUG, 'loaders': settings.TEMPLATE_LOADERS, 'string_if_invalid': settings.TEMPLATE_STRING_IF_INVALID, }, }, ] templates = OrderedDict() backend_names = [] for tpl in self._templates: tpl = tpl.copy() try: # This will raise an exception if 'BACKEND' doesn't exist or # isn't a string containing at least one dot. default_name = tpl['BACKEND'].rsplit('.', 2)[-2] except Exception: invalid_backend = tpl.get('BACKEND', '<not defined>') raise ImproperlyConfigured( "Invalid BACKEND for a template engine: {}. Check " "your TEMPLATES setting.".format(invalid_backend)) tpl.setdefault('NAME', default_name) tpl.setdefault('DIRS', []) tpl.setdefault('APP_DIRS', False) tpl.setdefault('OPTIONS', {}) templates[tpl['NAME']] = tpl backend_names.append(tpl['NAME']) counts = Counter(backend_names) duplicates = [alias for alias, count in counts.most_common() if count > 1] if duplicates: raise ImproperlyConfigured( "Template engine aliases aren't unique, duplicates: {}. " "Set a unique NAME for each engine in settings.TEMPLATES." .format(", ".join(duplicates))) return templates