本文整理匯總了Python中django.template.backends.django.DjangoTemplates方法的典型用法代碼示例。如果您正苦於以下問題:Python django.DjangoTemplates方法的具體用法?Python django.DjangoTemplates怎麽用?Python django.DjangoTemplates使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.template.backends.django
的用法示例。
在下文中一共展示了django.DjangoTemplates方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_default
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def get_default():
"""
Return the first DjangoTemplates backend that's configured, or raise
ImproperlyConfigured if none are configured.
This is required for preserving historical APIs that rely on a
globally available, implicitly configured engine such as:
>>> from django.template import Context, Template
>>> template = Template("Hello {{ name }}!")
>>> context = Context({'name': "world"})
>>> template.render(context)
'Hello world!'
"""
# Since Engine is imported in django.template and since
# DjangoTemplates is a wrapper around this Engine class,
# local imports are required to avoid import loops.
from django.template import engines
from django.template.backends.django import DjangoTemplates
for engine in engines.all():
if isinstance(engine, DjangoTemplates):
return engine.engine
raise ImproperlyConfigured('No DjangoTemplates backend is configured.')
示例2: test_context_has_priority_over_template_context_processors
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_context_has_priority_over_template_context_processors(self):
# See ticket #23789.
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {
'context_processors': [test_processor_name],
},
})
template = engine.from_string('{{ processors }}')
request = RequestFactory().get('/')
# Context processors run
content = template.render({}, request)
self.assertEqual(content, 'yes')
# Context overrides context processors
content = template.render({'processors': 'no'}, request)
self.assertEqual(content, 'no')
示例3: test_render_requires_dict
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_render_requires_dict(self):
"""django.Template.render() requires a dict."""
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {},
})
template = engine.from_string('')
context = Context()
request_context = RequestContext(RequestFactory().get('/'), {})
msg = 'context must be a dict rather than Context.'
with self.assertRaisesMessage(TypeError, msg):
template.render(context)
msg = 'context must be a dict rather than RequestContext.'
with self.assertRaisesMessage(TypeError, msg):
template.render(request_context)
示例4: test_builtins_discovery
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_builtins_discovery(self):
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {
'builtins': ['template_backends.apps.good.templatetags.good_tags'],
},
})
self.assertEqual(
engine.engine.builtins, [
'django.template.defaulttags',
'django.template.defaultfilters',
'django.template.loader_tags',
'template_backends.apps.good.templatetags.good_tags',
]
)
示例5: test_context_has_priority_over_template_context_processors
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_context_has_priority_over_template_context_processors(self):
# See ticket #23789.
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {
'context_processors': [test_processor_name],
},
})
template = engine.from_string('{{ processors }}')
request = self.request_factory.get('/')
# Context processors run
content = template.render({}, request)
self.assertEqual(content, 'yes')
# Context overrides context processors
content = template.render({'processors': 'no'}, request)
self.assertEqual(content, 'no')
示例6: test_render_requires_dict
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_render_requires_dict(self):
"""django.Template.render() requires a dict."""
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {},
})
template = engine.from_string('')
context = Context()
request_context = RequestContext(self.request_factory.get('/'), {})
msg = 'context must be a dict rather than Context.'
with self.assertRaisesMessage(TypeError, msg):
template.render(context)
msg = 'context must be a dict rather than RequestContext.'
with self.assertRaisesMessage(TypeError, msg):
template.render(request_context)
示例7: get_default
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def get_default():
"""
When only one DjangoTemplates backend is configured, returns it.
Raises ImproperlyConfigured otherwise.
This is required for preserving historical APIs that rely on a
globally available, implicitly configured engine such as:
>>> from django.template import Context, Template
>>> template = Template("Hello {{ name }}!")
>>> context = Context({'name': "world"})
>>> template.render(context)
'Hello world!'
"""
# Since Engine is imported in django.template and since
# DjangoTemplates is a wrapper around this Engine class,
# local imports are required to avoid import loops.
from django.template import engines
from django.template.backends.django import DjangoTemplates
django_engines = [engine for engine in engines.all()
if isinstance(engine, DjangoTemplates)]
if len(django_engines) == 1:
# Unwrap the Engine instance inside DjangoTemplates
return django_engines[0].engine
elif len(django_engines) == 0:
raise ImproperlyConfigured(
"No DjangoTemplates backend is configured.")
else:
raise ImproperlyConfigured(
"Several DjangoTemplates backends are configured. "
"You must select one explicitly.")
示例8: check_context_processors
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def check_context_processors(app_configs, **kwargs):
errors = []
if VERSION < (2, 2):
# The admin.E404 check has been introduced in the Django 2.2 cycle.
return errors
for engine in engines.all():
if isinstance(engine, DjangoTemplates):
django_templates_instance = engine.engine
break
else:
django_templates_instance = None
if django_templates_instance:
if (
"django.contrib.messages.context_processors.messages"
not in django_templates_instance.context_processors
and "admin.E404" not in settings.SILENCED_SYSTEM_CHECKS
):
errors.append(
checks.Error(
"If using 'user_messages.context_processors.messages'"
" instead of the official messages context processor"
" you have to add 'admin.E404' to SILENCED_SYSTEM_CHECKS.",
id="user_messages.E001",
)
)
return errors
示例9: get_template
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def get_template():
if this_module.template is None:
template_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'request_history.html'
)
with open(template_path) as template_file:
this_module.template = Template(
template_file.read(),
engine=DjangoTemplates({'NAME': 'rh', 'DIRS': [], 'APP_DIRS': False, 'OPTIONS': {}}).engine
)
return this_module.template
示例10: test_templatetag_discovery
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_templatetag_discovery(self):
engine = DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {
'libraries': {
'alternate': 'template_backends.apps.good.templatetags.good_tags',
'override': 'template_backends.apps.good.templatetags.good_tags',
},
},
})
# libraries are discovered from installed applications
self.assertEqual(
engine.engine.libraries['good_tags'],
'template_backends.apps.good.templatetags.good_tags',
)
self.assertEqual(
engine.engine.libraries['subpackage.tags'],
'template_backends.apps.good.templatetags.subpackage.tags',
)
# libraries are discovered from django.templatetags
self.assertEqual(
engine.engine.libraries['static'],
'django.templatetags.static',
)
# libraries passed in OPTIONS are registered
self.assertEqual(
engine.engine.libraries['alternate'],
'template_backends.apps.good.templatetags.good_tags',
)
# libraries passed in OPTIONS take precedence over discovered ones
self.assertEqual(
engine.engine.libraries['override'],
'template_backends.apps.good.templatetags.good_tags',
)
示例11: test_templatetag_discovery_import_error
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_templatetag_discovery_import_error(self):
"""
Import errors in tag modules should be reraised with a helpful message.
"""
with self.assertRaisesMessage(
InvalidTemplateLibrary,
"ImportError raised when trying to load "
"'template_backends.apps.importerror.templatetags.broken_tags'"
):
DjangoTemplates({
'DIRS': [],
'APP_DIRS': False,
'NAME': 'django',
'OPTIONS': {},
})
示例12: test_autoescape_default
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_autoescape_default(self):
templates = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
}]
engines = EngineHandler(templates=templates)
self.assertEqual(
engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
'Hello, Bob & Jim'
)
示例13: test_non_debug_default_template_loaders
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_non_debug_default_template_loaders(self):
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
self.assertEqual(engine.engine.loaders, [('django.template.loaders.cached.Loader', self.default_loaders)])
示例14: test_debug_default_template_loaders
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_debug_default_template_loaders(self):
engine = DjangoTemplates({'DIRS': [], 'APP_DIRS': True, 'NAME': 'django', 'OPTIONS': {}})
self.assertEqual(engine.engine.loaders, self.default_loaders)
示例15: test_autoescape_off
# 需要導入模塊: from django.template.backends import django [as 別名]
# 或者: from django.template.backends.django import DjangoTemplates [as 別名]
def test_autoescape_off(self):
templates = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {'autoescape': False},
}]
engines = EngineHandler(templates=templates)
self.assertEqual(
engines['django'].from_string('Hello, {{ name }}').render({'name': 'Bob & Jim'}),
'Hello, Bob & Jim'
)