You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using context processors with Jinja2 templates is discouraged.
Context processors are useful with Django templates because Django templates don’t support calling functions with arguments. Since Jinja2 doesn’t have that limitation, it’s recommended to put the function that you would use as a context processor in the global variables available to the template using jinja2.Environment as described below. You can then call that function in the template:
So, I suppose you've configured your jinja2 environment as described there
Well, lets modify this base example a little to add ratings tag:
In yourproject/jinja2.py:
from django.templatetags.static import static
from django.urls import reverse
from jinja2 import Environment, contextfunction
from star_ratings.templatetags.ratings import ratings
@contextfunction
def get_context(c):
return c
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': static,
'url': reverse,
'context': get_context,
'ratings': ratings,
})
return env
Thats all!
Now you can use ratings in your jinja2 templates, just simply:
How Do I Use this app with jinja2 Tempalte??
How can I do that?
Like {% load ratings %} doesn't exist in jinja2
The text was updated successfully, but these errors were encountered: