U
U
un1t2015-10-06 14:00:39
Django
un1t, 2015-10-06 14:00:39

Switching from Django Templates to Jinja2?

In Dzhang 1.8, it became possible to specify Jinja2 as a template engine. Of course, it used to be in the form of third-party applications, but now it's all out of the box. I'm thinking about switching to Jinja2.
Actually, what I don't like about jang templates is mainly speed, the rest is not so significant.
In this regard, such questions.
1. How to live without custom template tags? For example, various common menu-type blocks are implemented using custom tags. In jijnja2, the creation of custom tags is heavily confused, context processors are also missing.
2. How to live without context processors? request and so on every time hands to transfer to the view?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Klimenko, 2015-10-06
@un1t

the appearance of the official ability to connect jinja does not mean that the extensions became useless, when it was required, as a result, after weighing all the pros and cons, I still settled on niwinz.github.io/django-jinja
At the same time, I did not disable the standard templates, I use both, where templates became a bottleneck, switched to using jinja.
I use something like this:

TEMPLATES = [
    {
        'NAME': 'django',
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': False,
        'OPTIONS': {
            'string_if_invalid': 'VARIABLE ERROR',
            'context_processors': [
                "django.contrib.auth.context_processors.auth",
                "django.core.context_processors.request",
                'django.contrib.messages.context_processors.messages',
                'core.context_processors.cities_list',
                'django.core.context_processors.static',
            ],
            'loaders': [
                ('django.template.loaders.cached.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],

        },
    },
    {
        'NAME': 'jinja2',
        'BACKEND': 'django_jinja.backend.Jinja2',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'autoescape': False,  # !!!!!!
            "app_dirname": "jinja2",
            "match_extension": ".html",
            "newstyle_gettext": True,
            "translation_engine": "django.utils.translation",
            "extensions": [
                "jinja2.ext.do",
                "jinja2.ext.loopcontrols",
                "jinja2.ext.with_",
                "jinja2.ext.i18n",
                "jinja2.ext.autoescape",
                "django_jinja.builtins.extensions.CsrfExtension",
                "django_jinja.builtins.extensions.CacheExtension",
                "django_jinja.builtins.extensions.TimezoneExtension",
                "django_jinja.builtins.extensions.UrlsExtension",
                "django_jinja.builtins.extensions.StaticFilesExtension",
                "django_jinja.builtins.extensions.DjangoFiltersExtension",
                # "coffin.spaceless" не работает в py3
            ],
        }
    },
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question