Exemple #1
0
    def get_template(self, template, def_name=None):
        '''Retrieve a *Django* API template object for the given template name, using the app_path and template_subdir
           settings in this object.  This method still uses the corresponding Mako template and engine, but it
           gives a Django API wrapper around it so you can use it the same as any Django template.

           If def_name is provided, template rendering will be limited to the named def/block (see Mako docs).

           This method corresponds to the Django templating system API.
           A Django exception is raised if the template is not found or cannot compile.
        '''
        try:
            # wrap the mako template in an adapter that gives the Django template API
            return MakoTemplateAdapter(self.get_mako_template(template),
                                       def_name)

        except (TopLevelLookupException,
                TemplateLookupException) as e:  # Mako exception raised
            tdne = TemplateDoesNotExist(
                'Template "%s" not found in search path: %s.' %
                (template, self.template_search_dirs))
            if settings.DEBUG:
                tdne.template_debug = get_template_debug(template, e)
            raise tdne from e

        except (CompileException,
                SyntaxException) as e:  # Mako exception raised
            tse = TemplateSyntaxError('Template "%s" raised an error: %s' %
                                      (template, e))
            if settings.DEBUG:
                tse.template_debug = get_template_debug(template, e)
            raise tse from e
Exemple #2
0
    def get_template(self, template_name):
        if not self.match_template(template_name):
            message = "Template {} does not exists".format(template_name)
            raise TemplateDoesNotExist(message)

        try:
            return Template(self.env.get_template(template_name), self)
        except jinja2.TemplateNotFound as exc:
            # Unlike django's template engine, jinja2 doesn't like windows-style path separators.
            # But because django does, its docs encourage the usage of os.path.join().
            # Rather than insisting that our users switch to posixpath.join(), this try block
            # will attempt to retrieve the template path again with forward slashes on windows:
            if os.name == 'nt' and '\\' in template_name:
                try:
                    return self.get_template(template_name.replace("\\", "/"))
                except jinja2.TemplateNotFound:
                    pass

            exc = TemplateDoesNotExist(exc.name, backend=self)

            utils.reraise(
                TemplateDoesNotExist,
                exc,
                sys.exc_info()[2],
            )
        except jinja2.TemplateSyntaxError as exc:
            new = TemplateSyntaxError(exc.args)
            new.template_debug = get_exception_info(exc)
            utils.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
Exemple #3
0
    def get_template(self, template, def_name=None):
        '''Retrieve a *Django* API template object for the given template name, using the app_path and template_subdir
           settings in this object.  This method still uses the corresponding Mako template and engine, but it
           gives a Django API wrapper around it so you can use it the same as any Django template.

           If def_name is provided, template rendering will be limited to the named def/block (see Mako docs).

           This method corresponds to the Django templating system API.
           A Django exception is raised if the template is not found or cannot compile.
        '''
        try:
            # wrap the mako template in an adapter that gives the Django template API
            return MakoTemplateAdapter(self.get_mako_template(template), def_name)

        except (TopLevelLookupException, TemplateLookupException) as e: # Mako exception raised
            tdne = TemplateDoesNotExist('Template "%s" not found in search path: %s.' % (template, self.template_search_dirs))
            if settings.DEBUG:
                tdne.template_debug = get_template_debug(template, e)
            raise tdne from e

        except (CompileException, SyntaxException) as e: # Mako exception raised
            tse = TemplateSyntaxError('Template "%s" raised an error: %s' % (template, e))
            if settings.DEBUG:
                tse.template_debug = get_template_debug(template, e)
            raise tse from e
Exemple #4
0
 def get_template(self, template_name):
     try:
         return Template(self.env.get_template(template_name), self)
     except jinja2.TemplateNotFound as exc:
         raise TemplateDoesNotExist(exc.name, backend=self) from exc
     except jinja2.TemplateSyntaxError as exc:
         new = TemplateSyntaxError(exc.args)
         new.template_debug = get_exception_info(exc)
         raise new from exc
Exemple #5
0
 def get_template(self, template_name):
     try:
         return Template(self.env.get_template(template_name), self)
     except jinja2.TemplateNotFound as exc:
         raise TemplateDoesNotExist(exc.name, backend=self) from exc
     except jinja2.TemplateSyntaxError as exc:
         new = TemplateSyntaxError(exc.args)
         new.template_debug = get_exception_info(exc)
         raise new from exc
Exemple #6
0
 def get_template(self, template_name):
     try:
         return Template(self.env.get_template(template_name))
     except jinja2.TemplateNotFound as exc:
         six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.name, backend=self), sys.exc_info()[2])
     except jinja2.TemplateSyntaxError as exc:
         new = TemplateSyntaxError(exc.args)
         new.template_debug = get_exception_info(exc)
         six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
Exemple #7
0
 def get_template(self, template_name):
     try:
         return Template(self.env.get_template(template_name))
     except jinja2.TemplateNotFound as exc:
         six.reraise(
             TemplateDoesNotExist,
             TemplateDoesNotExist(exc.name, backend=self),
             sys.exc_info()[2],
         )
     except jinja2.TemplateSyntaxError as exc:
         new = TemplateSyntaxError(exc.args)
         new.template_debug = get_exception_info(exc)
         six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
Exemple #8
0
 def render(self, context=None, request=None):
     from .utils import csrf_input_lazy, csrf_token_lazy
     if context is None:
         context = {}
     if request is not None:
         context['request'] = request
         context['csrf_input'] = csrf_input_lazy(request)
         context['csrf_token'] = csrf_token_lazy(request)
         for context_processor in self.backend.template_context_processors:
             context.update(context_processor(request))
     try:
         return self.template.render(context)
     except jinja2.TemplateSyntaxError as exc:
         new = TemplateSyntaxError(exc.args)
         new.template_debug = get_exception_info(exc)
         raise new from exc
Exemple #9
0
    def get_template(self, template_name):
        if not self.match_template(template_name):
            message = "Template {} does not exists".format(template_name)
            raise TemplateDoesNotExist(message)

        try:
            return Template(self.env.get_template(template_name), self)
        except jinja2.TemplateNotFound as exc:
            six.reraise(
                TemplateDoesNotExist,
                TemplateDoesNotExist(exc.name, backend=self),
                sys.exc_info()[2],
            )
        except jinja2.TemplateSyntaxError as exc:
            new = TemplateSyntaxError(exc.args)
            new.template_debug = get_exception_info(exc)
            six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
    def get_template(self, template_name):
        if not self.match_template(template_name):
            message = "Template {} does not exists".format(template_name)
            raise TemplateDoesNotExist(message)

        try:
            return Template(self.env.get_template(template_name), self)
        except jinja2.TemplateNotFound as exc:
            six.reraise(
                TemplateDoesNotExist,
                TemplateDoesNotExist(exc.name, backend=self),
                sys.exc_info()[2],
            )
        except jinja2.TemplateSyntaxError as exc:
            new = TemplateSyntaxError(exc.args)
            new.template_debug = get_exception_info(exc)
            six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
# Since this package contains a "django" module, this is required on Python 2.