eikonomega / django-notification-system

Want to send notifications from your Django app? Start here.
MIT License
14 stars 9 forks source link

How to provide Email Templates? #19

Closed nklsw closed 3 years ago

nklsw commented 3 years ago

I have not yet managed to include an email template via the extra dict.

I am creating a Notification object like this:

User = get_user_model()
user = User.objects.get(first_name="Eggs", last_name="Benedict")

someval = 'something'

create_notification(
        user=user,
        title='Email Title Text',
        extra={
            "someval": "someval",
            "template_name": "eggs_email_template.html"
        }
    )

The template HTML file is in the correct directory (also tried some random possibilities). If I specify only the template_name, it does not throw an error, but it also does not create a notification. If I specify other context values in the extra dict , I get the following error:

render() got an unexpected keyword argument 'someval'

Could you please provide an example of how to include templates?

branks42 commented 3 years ago

Thanks for bringing this to our attention!

A few things of note: We are using get_template to render the templates. This will look through the DIRS listed in TEMPLATES of your settings file. You will need to specify in there where your templates are stored. Something like this:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # RIGHT HERE ▼
        "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",
            ],
        },
    }

This should fix the issue you're having. I also noticed that inside our django_notification_system.notification_creators.email.create_notification function there was a small issue on line 76. The asterisks need to be removed.

email_body = template.render(**extra)

becomes

email_body = template.render(extra)

This has been patched and built if you pull the latest code, but if you're just playing around with it locally you can drop those two asterisks and everything should work fine.

nklsw commented 3 years ago

Thanks @branks42, that solved the problem for me.

Keep up the good work! This is a very useful project.