tutorcruncher/django-jinja-bootstrap-form

Detailed configuration / setting up

Closed this issue · 2 comments

Hi there,

Thanks for making this library.

I was expecting to use the lib by simply pip installing it and adding bootstrapform_jinja to INSTALLED_APPS, but unfortunately this plug-in-and-play doesn't seem to work in my environment.

I got the following 2 issues:

  1. The filters ( bootstrap, etc ) are not registered to the Jinja Environment object, which caused the error ("No filter named 'bootstrap'.",)
  2. After manual registering of the filters, bootstrapform_jinja was not processed by the Jinja backend, which caused the error Invalid block tag on line 1: 'import'. Did you forget to register or load this tag?

My Jinja configuration was based on the example in the Django official doc here
https://docs.djangoproject.com/en/4.1/topics/templates/#django.template.backends.jinja2.Jinja2

from django.templatetags.static import static
from django.urls import reverse
from jinja2 import Environment

def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': static,
        'url': reverse,
    })
    return env

And this environment was registered in the Django TEMPLATES settings like

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        ...
        'OPTIONS': {
            'environment': '__path_to_the_above_file__.environment',
            ...
        },
    },
   ...
]

To solve 1st issue, I tried attaching the internal filters to the env returned, like this

from django_jinja.library import _update_env

def environment(**options):
    env = Environment(**options)
    # ...
    _update_env(env)
    return env

Now came the 2nd error. This time I tried adding the path of bootstrapform_jinja/templates in TEMPLATES, like this

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': ['/absolute/path/to/venv/site-packages/bootstrapform_jinja/templates',
                 'TEMPLATES FOLDER OF OTHER APPS', ...],
    },
   ...
]

And now it works. Though, neither of those changes looks good to be pushed into production environment.

Wonder how I was supposed to use this library. Any help is appreciated.


UPDATE:

I was using Python 3.9.7, along with

asgiref==3.5.2
Babel==2.10.3
Django==4.1.1
django-filter==22.1
django-jinja==2.10.2
django-jinja-bootstrap-form==4.3.5
Jinja2==3.1.2
MarkupSafe==2.1.1
pytz==2022.2.1
sqlparse==0.4.2

I found what was wrong. I was trying to use the built-in backend django.template.backends.jinja2.Jinja2. It is not compatible with the backend provided by django_jinja.

For using this lib with the built-in backend, I made a workaround (an Django app) to solve the second issue.

from pathlib import Path

from django.apps import AppConfig
from django.conf import settings


class BootstrapFormAdapterConfig(AppConfig):
    name = 'bootstrap_form_adapter'

    def ready(self):
        from bootstrapform_jinja import apps as bootstrap_form_jinja_apps
        from bootstrapform_jinja.templatetags import bootstrap # NOQA

        for template_settings in settings.TEMPLATES:
            if template_settings['BACKEND'].endswith('Jinja2'):
                template_settings['DIRS'] = (
                    Path(bootstrap_form_jinja_apps.__file__).resolve().parent / 'templates',
                    *template_settings['DIRS'],
                )
                break

And adding this bootstrap_form_adapter app instead of bootstrapform_jinja in INSTALLED_APPS makes the templates rendered fine.

Feel free to close this ticket if you don't have plan to support the built-in one.

Sorry for the delay, I haven't had the time to monitor this repo.

You are right, we'll just support django_jinja for now.