How to use the jinja2.TemplateNotFound function in Jinja2

To help you get started, we’ve selected a few Jinja2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kamalgill / flask-appengine-template / src / lib / flask / testsuite / blueprints.py View on Github external
self.assert_equal(rv.data, b'Hello from the Admin')
        rv = c.get('/admin/static/test.txt')
        self.assert_equal(rv.data.strip(), b'Admin File')
        rv.close()
        rv = c.get('/admin/static/css/test.css')
        self.assert_equal(rv.data.strip(), b'/* nested file */')
        rv.close()

        with app.test_request_context():
            self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
                              '/admin/static/test.txt')

        with app.test_request_context():
            try:
                flask.render_template('missing.html')
            except TemplateNotFound as e:
                self.assert_equal(e.name, 'missing.html')
            else:
                self.assert_true(0, 'expected exception')

        with flask.Flask(__name__).test_request_context():
            self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
github apache / allura / Allura / allura / lib / package_path_loader.py View on Github external
def get_source(self, environment, template):
        '''
        Returns the source for jinja2 rendered templates. Can understand...
        - path/to/template.html
        - module:path/to/template.html
        '''
        # look in all of the customized search locations...
        if not asbool(config.get('disable_template_overrides', False)):
            try:
                parts = [self.override_root] + template.split(':')
                if len(parts) > 2:
                    parts[1:2] = parts[1].split('.')
                return self.fs_loader.get_source(environment,
                                                 os.path.join(*parts))
            except jinja2.TemplateNotFound:
                # fall-back to attempt non-override loading
                pass

        if ':' in template:
            package, path = template.split(':', 2)
            filename = pkg_resources.resource_filename(package, path)
            return self.fs_loader.get_source(environment, filename)
        else:
            return self.fs_loader.get_source(environment, template)
github mkerrin / pwt.jinja2js / src / pwt / jinja2js / wsgi.py View on Github external
def __call__(self, request):
        path = request.path_info

        try:
            source, filename, uptodate = self.env.loader.get_source(
                self.env, path)
        except jinja2.TemplateNotFound:
            return webob.Response("Not found", status = 404)

        node = self.env._parse(source, path, filename)

        output = self.compiler(node, self.env, path, filename)

        return webob.Response(
            body = output, content_type = "application/javascript")
github nexB / aboutcode-toolkit / about_code_tool / about.py View on Github external
'attribution texts. You can install it by running:'
                  '"configure"')
            return

        if not template_path:
            template_path = join(dirname(realpath(__file__)),
                                 "templates/default.html")

        # FIXME: the template dir should be outside the code tree
        template_dir = dirname(template_path)
        template_file_name = basename(template_path)
        loader = j2.FileSystemLoader(template_dir)
        jinja_env = j2.Environment(loader=loader)
        try:
            template = jinja_env.get_template(template_file_name)
        except j2.TemplateNotFound:
            return
        limit_to = limit_to or []

        about_object_fields = []
        license_dict = {}

        not_process_components = list(limit_to)
        component_exist = False

        if limit_to:
            for component in not_process_components:
                for about_object in self:
                    # The about_object.location is the absolute path of the ABOUT
                    # file. The purpose of the following string partition is to 
                    # match the about_file's location with the input list.
                    about_relative_path = about_object.location.partition(
github binaryage / drydrop / dryapp / drydrop / app / core / controller.py View on Github external
def render(self, template_name):
        env = jinja2.Environment(loader = InternalTemplateLoader(os.path.join(DRY_ROOT, 'app', 'views')))
        try:
            template = env.get_template(template_name)
        except jinja2.TemplateNotFound:
            raise jinja2.TemplateNotFound(template_name)
        content = template.render(self.view)
        if LOCAL:
            content = cache_buster(content)
        self.response.out.write(content)
github holdenrehg / sample_flask_stripe_integration / mystripeapp / routes / dashboard.py View on Github external
def dashboard():
    try:
        return render_template("views/dashboard.html", user=current_user)
    except TemplateNotFound:
        abort(404)
github niwinz / django-jinja / django_jinja / backend.py View on Github external
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:

            if utils.DJANGO_18:
                exc = TemplateDoesNotExist(exc.name)
            else:
                exc = TemplateDoesNotExist(exc.name, backend=self)

            six.reraise(
                TemplateDoesNotExist,
                exc,
                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])
github Antergos / antbs / antbs / views / __init__.py View on Github external
def try_render_template(*args, **kwargs):
    try:
        return render_template(*args, **kwargs)
    except TemplateNotFound:
        abort(500)
github yunstanford / jinja2-sanic / jinja2_sanic / __init__.py View on Github external
:param template_name: template name.
    :param request: a parameter from web-handler, sanic.request.Request instance.
    :param context: context for rendering.
    :param app_key: a optional key for application instance. If not provided,
                    default value will be used.
    """
    env = get_env(request.app, app_key=app_key)
    if not env:
        raise ServerError(
            "Template engine has not been initialized yet.",
            status_code=500,
        )
    try:
        template = env.get_template(template_name)
    except jinja2.TemplateNotFound as e:
        raise ServerError(
            "Template '{}' not found".format(template_name),
            status_code=500,
        )
    if not isinstance(context, Mapping):
        raise ServerError(
            "context should be mapping, not {}".format(type(context)),
            status_code=500,
        )
    if request.get(REQUEST_CONTEXT_KEY):
        context = dict(request[REQUEST_CONTEXT_KEY], **context)
    text = template.render(context)
    return text
github pengutronix / flamingo / flamingo / core / templating / jinja2.py View on Github external
]

        if isinstance(exception, TemplateSyntaxError):
            # in case of a TemplateSyntaxError jinja2 adds the current
            # template to the traceback

            if len(stack) > 1 and stack[0] == stack[-1]:
                stack = stack[:-1]

        template = self.error_env.get_template('error.html')

        return template.render(
            context=self.context,
            exception=exception,
            stack=stack,
            TemplateNotFound=TemplateNotFound,
            isinstance=isinstance,
        )