本文整理匯總了Python中django.template.engines方法的典型用法代碼示例。如果您正苦於以下問題:Python template.engines方法的具體用法?Python template.engines怎麽用?Python template.engines使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.template
的用法示例。
在下文中一共展示了template.engines方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setUp
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def setUp(self):
super(TestMMVHTMLRenderer, self).setUp()
"""
Monkeypatch get_template
Taken from DRF Tests
"""
self.get_template = django.template.loader.get_template
def get_template(template_name, dirs=None):
if template_name == 'test.html':
return engines['django'].from_string("<html>test: {{ data }}</html>")
raise TemplateDoesNotExist(template_name)
def select_template(template_name_list, dirs=None, using=None):
if template_name_list == ['test.html']:
return engines['django'].from_string("<html>test: {{ data }}</html>")
raise TemplateDoesNotExist(template_name_list[0])
django.template.loader.get_template = get_template
django.template.loader.select_template = select_template
示例2: setUp
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def setUp(self):
self.engine = engines['jinja2']
self.image = Image.objects.create(
title="Test image",
file=get_test_image_file(),
)
# Create an image with a missing file, by deserializing fom a python object
# (which bypasses FileField's attempt to read the file)
self.bad_image = list(serializers.deserialize('python', [{
'fields': {
'title': 'missing image',
'height': 100,
'file': 'original_images/missing-image.jpg',
'width': 100,
},
'model': 'wagtailimages.image'
}]))[0].object
self.bad_image.save()
示例3: test_full_dec_templateresponse
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def test_full_dec_templateresponse(self):
"""
All methods of middleware are called for TemplateResponses in
the right sequence.
"""
@full_dec
def template_response_view(request):
template = engines['django'].from_string("Hello world")
return TemplateResponse(request, template)
request = self.rf.get('/')
response = template_response_view(request)
self.assertTrue(getattr(request, 'process_request_reached', False))
self.assertTrue(getattr(request, 'process_view_reached', False))
self.assertTrue(getattr(request, 'process_template_response_reached', False))
# response must not be rendered yet.
self.assertFalse(response._is_rendered)
# process_response must not be called until after response is rendered,
# otherwise some decorators like csrf_protect and gzip_page will not
# work correctly. See #16004
self.assertFalse(getattr(request, 'process_response_reached', False))
response.render()
self.assertTrue(getattr(request, 'process_response_reached', False))
# process_response saw the rendered content
self.assertEqual(request.process_response_content, b"Hello world")
示例4: test_patch_vary_headers
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def test_patch_vary_headers(self):
headers = (
# Initial vary, new headers, resulting vary.
(None, ('Accept-Encoding',), 'Accept-Encoding'),
('Accept-Encoding', ('accept-encoding',), 'Accept-Encoding'),
('Accept-Encoding', ('ACCEPT-ENCODING',), 'Accept-Encoding'),
('Cookie', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
('Cookie, Accept-Encoding', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
(None, ('Accept-Encoding', 'COOKIE'), 'Accept-Encoding, COOKIE'),
('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
('Cookie , Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
)
for initial_vary, newheaders, resulting_vary in headers:
with self.subTest(initial_vary=initial_vary, newheaders=newheaders):
template = engines['django'].from_string("This is a test")
response = TemplateResponse(HttpRequest(), template)
if initial_vary is not None:
response['Vary'] = initial_vary
patch_vary_headers(response, newheaders)
self.assertEqual(response['Vary'], resulting_vary)
示例5: test_get_cache_key
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def test_get_cache_key(self):
request = self.factory.get(self.path)
template = engines['django'].from_string("This is a test")
response = TemplateResponse(HttpRequest(), template)
key_prefix = 'localprefix'
# Expect None if no headers have been set yet.
self.assertIsNone(get_cache_key(request))
# Set headers to an empty list.
learn_cache_key(request, response)
self.assertEqual(
get_cache_key(request),
'views.decorators.cache.cache_page.settingsprefix.GET.'
'58a0a05c8a5620f813686ff969c26853.d41d8cd98f00b204e9800998ecf8427e'
)
# A specified key_prefix is taken into account.
learn_cache_key(request, response, key_prefix=key_prefix)
self.assertEqual(
get_cache_key(request, key_prefix=key_prefix),
'views.decorators.cache.cache_page.localprefix.GET.'
'58a0a05c8a5620f813686ff969c26853.d41d8cd98f00b204e9800998ecf8427e'
)
示例6: _get_template_loaders
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def _get_template_loaders():
"""
Get all available template loaders for the Django engine.
"""
loaders = []
for loader_name in engines['django'].engine.loaders:
loader = engines['django'].engine.find_template_loader(loader_name)
if loader is not None and hasattr(loader, 'get_template_sources'):
loaders.append(loader)
return tuple(loaders)
示例7: render_template
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def render_template(value, **context):
template = engines["django"].from_string(value)
request = context.pop("request", None)
return template.render(context, request)
示例8: test_get_last_invalidation_jinja2
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def test_get_last_invalidation_jinja2(self):
original_timestamp = engines['jinja2'].from_string(
"{{ timestamp }}"
).render({
'timestamp': get_last_invalidation('auth.Group', 'cachalot_test'),
})
template = engines['jinja2'].from_string(
"{{ get_last_invalidation('auth.Group', 'cachalot_test') }}")
timestamp = template.render({})
self.assertNotEqual(timestamp, '')
self.assertNotEqual(timestamp, '0.0')
self.assertAlmostEqual(float(timestamp), float(original_timestamp),
delta=0.1)
示例9: render_template
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def render_template(value, **context):
template = engines['django'].from_string(value)
request = context.pop('request', None)
return template.render(context, request)
示例10: render_template
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def render_template(self, template_string, context={}, using='tex'):
engine = engines[using]
template = engine.from_string(template_string)
return template.render(context)
示例11: setUp
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def setUp(self):
self.engine = engines['jinja2']
self.user = get_user_model().objects.create_superuser(
username='test',
email='test@email.com',
password='password'
)
self.homepage = Page.objects.get(id=2)
示例12: setUp
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def setUp(self):
super().setUp()
self.engine = engines['jinja2']
示例13: setUp
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def setUp(self):
self.engine = engines['jinja2']
示例14: COMPRESS_JINJA2_GET_ENVIRONMENT
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def COMPRESS_JINJA2_GET_ENVIRONMENT():
from django.template import engines
return engines["jinja"].env
示例15: template
# 需要導入模塊: from django import template [as 別名]
# 或者: from django.template import engines [as 別名]
def template(request):
template = engines["django"].from_string(
"Hello {% block name %}{{ name }}{% endblock %}!"
)
context = {"name": "World"}
return HttpResponse(template.render(context))