safwanrahman / django-webpush

Web Push Notification Package for Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problems to load 'webpush_header'

tiagoavsilva opened this issue · comments

I'm trying to integrate webpush into my Django project following README.md but I'm having problems. In the description it says 'django-webpush is shipped with built in jinja support'. Do I need to make any changes in my project to enable 'built in jinja support'?

When I include {% webpush_header %} in my template I'm getting the following error:

Invalid block tag on line 12: 'webpush_header'. Did you forget to register or load this tag?

Could someone tell me what I'm doing wrong?

django-webpush is shipped with built in jinja support. If you would like to use with jinja backend, pass pipeline.jinja2.PipelineExtension to your jinja environment. Like following:

{
    "BACKEND": "django_jinja.backend.Jinja2",
    "OPTIONS": {
      'extensions': ['webpush.jinja2.WebPushExtension'],
    }
},

Adding this code to my settings.py I'm getting the following error:

File "...\venv\lib\site-packages\django_jinja\backend.py", line 193, in __init__
      self.env.install_gettext_translations (translation, newstyle = newstyle_gettext)
AttributeError: 'Environment' object has no 'install_gettext_translations' attribute

@tiagoavsilva Seems like you need to add the default extensions like it is mentioned in the django-jinja documentation.

So it will be

from django_jinja.builtins import DEFAULT_EXTENSIONS

"OPTIONS": {
    "extensions": DEFAULT_EXTENSIONS + [
       'webpush.jinja2.WebPushExtension'
    ]
}

@tiagoavsilva I have fixed the documentation of this project as well. I think it is more explicit now.

@safwanrahman I solved the problem using Django's Templating. But for that I needed to add the 'builtins' with webpush in default TEMPLATES:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'builtins': [
                'webpush.templatetags.webpush_notifications',
            ],
        },
    },
]

It might be interesting to add this to the documentation as well.