niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
363 stars 102 forks source link

Django1.9 with django-jinja not working. Django is not using jinja2 template loader #199

Closed abhimanyub closed 7 years ago

abhimanyub commented 7 years ago

screen shot 2017-04-27 at 12 41 05 pm

After setting up as mentioned in the quick start quide. Django is still not using Jinja2 as the default template engine.

My setting :

TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        "APP_DIRS": True,
        "OPTIONS": {
            "match_extension": ".jinja",
            "app_dirname": "templates",
            "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",
            ],
            "bytecode_cache": {
                "name": "default",
                "backend": "django_jinja.cache.BytecodeCache",
                "enabled": False,
            },
        }
    },
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        'DIRS':['/---/---/---/templates'],
        "APP_DIRS": True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.request',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.core.context_processors.static',
                # 'webpack_loader.contrib.jinja2ext.WebpackExtension',
            ],
        },
    },
]

Kindly help. Thanks

niwinz commented 7 years ago

Do you have django_jinja on INSTALLED_APPS ?

In any case, seems like a configuration issue (something that depends on the environment on you are runing your, I don't know).

abhimanyub commented 7 years ago

Yes I've added that to my installed apps right before the template configuration. Anything else I need to update.

I'm using Django 1.9.5 django-jinja 2.2.1 Jinja2 2.7

I am able to use Jinja and load the template explicitly using jinja2 backend. But, I want to use django-jinja

niwinz commented 7 years ago

I don't know how I can help you. Something is wrong in your config or environment or both. On my tests on my own projects and the test suite of the django-jinja, everything should work as expected.

srivatsshankar commented 5 years ago

@abhimanyub I do not know if this would help you now, since this has been quite a while. However, I had a similar problem (albeit with Django 2.1) and the following configuration helped me figure out how to use django-jinja.

Essentially, the problem is that django-jinja does not automatically identify the relevant template directly used by the default template engine. You need to explicitly declare a value for "DIRS". Secondly, "match_extension" needs to contain all of the template extensions that would be used, ideally, HTML (you do not need the ".jinja" extension but you can leave it in if you like).

To give you an example of how I got the system to work, here is my configuration:

{
        "BACKEND": "django_jinja.backend.Jinja2",
        'DIRS': [os.path.join(BASE_DIR, 'templates')], # This needs to be explicitly declared
        "APP_DIRS": True,
        "OPTIONS": {
            "match_extension": (
                        ".html", # Remember to include the default extension of your template files
                        ".jinja",
            ), # This is a tuple, do not use a list
            "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",
            ],
            "bytecode_cache": {
                "name": "default",
                "backend": "django_jinja.cache.BytecodeCache",
                "enabled": False,
            },
        }
    },

I hope this helps someone. Cheers!

abhimanyub commented 5 years ago

Thanks for the help. We solved this problem by a similar approach. Thanks again :)

cristihainic commented 1 year ago

@abhimanyub what was your approach? I'm having the same issue and @srivatsshankar 's suggestion does not help. Thank you