miracle2k / django-assets

Django webassets integration.
BSD 2-Clause "Simplified" License
89 stars 79 forks source link

Does not work with Jinja2 out of the box #82

Open IlyaSemenov opened 8 years ago

IlyaSemenov commented 8 years ago

Problem

The current documentation misleads that django-assets will work with Django and Jinja2. It will not, not out of the box at least.

Steps to reproduce

  1. Add 'webassets' and 'django_assets' to INSTALLED_APPS
  2. Add 'django_jinja.backend.Jinja2' (or 'django.template.backends.jinja2') to TEMPLATES
  3. Add 'webassets.ext.jinja2.AssetsExtension' to Jinja2 extensions
  4. Add {% assets "js/main.js", output="min/main.js" %}{% endassets %} to your template file

    What happens

File "/Users/semenov/test/var/venv/lib/python3.5/site-packages/webassets/ext/jinja2.py" in _render_assets

Exception Type: RuntimeError at /test/
Exception Value: No assets environment configured in Jinja2 environment

Expected

The environment should be automatically configured; or it should be clearly documented how to configure it manually.

Current workaround

Create a custom extension:

# myapp/jinja2_assets.py

from jinja2.ext import Extension
from django_assets.env import get_env

class AssetsExtension(Extension):
    def __init__(self, environment):
        super().__init__(environment)
        environment.assets_environment = get_env()

then add myapp.jinja2_assets.AssetsExtension to the list of Jinja2 extensions.

fpuga commented 6 years ago

I also fall into this. Thanks a lot for the workaround.