我们从Python开源项目中,提取了以下25个代码示例,用于说明如何使用django.template.engines()。
def test_django_popup_view_field_javascript(self): template_code = """ {% load django_popup_view_field_tags %} {% django_popup_view_field_javascript %} """ template = engines['django'].from_string(template_code) html = template.render({}) self.assertInHTML('''<script type="text/javascript" src="/django_popup_view_field/jsi18n/"></script>''', html) self.assertInHTML(''' <script type="text/javascript" src="/static/django_popup_view_field/js/django_popup_view_field.min.js"> </script> ''', html) assert html.find('''id="template-django-popup-view-field"''') != -1
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()
def test_get_thumbnail_lazy(self): self.create_thumbnail_options() base_image = self.create_filer_image_object() context = { 'image': base_image } request = self.get_request(page=None, lang='en', path='/') template = ( '{% load filer_celery %}\n' '{% generate_thumbnail image size="20x20" ' 'crop=True subject_location=image.subject_location as image_big %}\n' '{% generate_thumbnail image size="100x20" ' 'crop=False subject_location=image.subject_location %}\n' '{% generate_thumbnail image alias="1" ' 'subject_location=image.subject_location%}\n' '-{{ image_big.url }}-' ) template_obj = engines['django'].from_string(template) output_first_pass = template_obj.render(context, request) output_second_pass = template_obj.render(context, request) opts = ( {'size': (20, 20), 'crop': True}, {'size': (100, 20), 'crop': False}, ) self.assertFalse(output_first_pass == output_second_pass) for opt in opts: existing = get_thumbnailer(base_image).get_existing_thumbnail(opt) self.assertTrue(existing) self.assertTrue(os.path.exists(existing.path)) self.assertTrue(output_second_pass.find(existing.url) > -1) opt['quality'] = 10 existing = get_thumbnailer(base_image).get_existing_thumbnail(opt) self.assertTrue(existing) self.assertTrue(os.path.exists(existing.path)) self.assertTrue(output_first_pass.find(existing.url) > -1)
def render_template(text, context=None): """ Create a template ``text`` that first loads bootstrap4. """ template = engines['django'].from_string(text) if not context: context = {} return template.render(context)
def _get_template(template_string): if __is_18: return engines['django'].from_string(template_string) else: return loader.get_template_from_string(template_string)
def COMPRESS_JINJA2_GET_ENVIRONMENT(): from django.template import engines return engines["jinja"].env # static files location
def rendered_recording_iframe(self): if not (self.recording_url and self.recording_source): return from django.template import engines django_engine = engines['django'] template = django_engine.from_string('<iframe width="100%" height="380px" src="{{ url }}" frameborder="0" allowfullscreen></iframe>') return template.render(context={'url': self.recording_url})
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)
def render_template_obj(self, template, context, request): template_obj = engines['django'].from_string(template) return template_obj.render(context, request)
def get_context(): if engines is not None: context = Context() context.template = Template('') return context else: return {}
def JINJA2_GET_ENVIRONMENT(): alias = 'jinja2' try: from django.template import engines return engines[alias].env except InvalidTemplateEngineError: raise InvalidTemplateEngineError( "Could not find config for '{}' " "in settings.TEMPLATES. " "COMPRESS_JINJA2_GET_ENVIRONMENT() may " "need to be defined in settings".format(alias)) except ImportError: return None
def email_activists_preview(self, request, pk=None): action_obj = self.get_object() serializer = serializers.EmailSerializer(data={ 'subject': request.data.get('subject', None), 'body': request.data.get('body', None), 'signups': request.data.get('signups', None) }) if serializer.is_valid(): templated_body = engines['django'].from_string(serializer.validated_data.get('body')) email_template = loader.get_template('email.eml') signups = models.Signup.objects.filter( action=action_obj, pk__in=serializer.validated_data.get('signups') ) s = signups[0] cxt = { 'action': action_obj, 'signup': s, 'activist': s.activist } generated_email = email_template.render({ 'signup': s, 'body': templated_body.render(cxt) }) return Response({'body': generated_email}) else: return Response({'errors': serializer.errors}, 400)
def email_activists(self, request, pk=None): action_obj = self.get_object() serializer = serializers.EmailSerializer(data={ 'subject': request.data.get('subject', None), 'body': request.data.get('body', None), 'signups': request.data.get('signups', None) }) if serializer.is_valid(): templated_body = engines['django'].from_string(serializer.validated_data.get('body')) email_template = loader.get_template('email.eml') signups = models.Signup.objects.filter( action=action_obj, pk__in=serializer.validated_data.get('signups') ) for s in signups: cxt = { 'action': action_obj, 'signup': s, 'activist': s.activist } generated_email = email_template.render({ 'signup': s, 'body': templated_body.render(cxt) }) email_obj = EmailMessage( subject=serializer.validated_data.get('subject'), body=generated_email, to=[s.activist.email], reply_to=[request.user.email], ) email_obj.encoding = 'utf-8' email_obj.send() return Response({'body': generated_email}) else: return Response({'errors': serializer.errors}, 400)
def render_notice_message(template, dispatcher, **context): engine = engines['notices'] return engine.from_string(template).render(dict( category=dispatcher.category, **context))
def render_template(text, context=None): """ Create a template ``text`` that first loads bootstrap3. """ template = engines['django'].from_string(text) if not context: context = {} return template.render(context)
def render_template(value, **context): template = engines['django'].from_string(value) request = context.pop('request', None) return template.render(context, request)
def render_template(text, context=None): """ Create a template ``text`` that first loads boilerplate. """ try: template = engines['django'].from_string(text) except Exception: template = Engine().from_string(text) if not context: context = {} return template.render(context)
def render_template_string(request, html, context=None): """Take a template in the form of a string and render it for the given context""" template = engines['django'].from_string(html) return template.render(context=context, request=request)