首页 > 解决方案 > 如何在 django 中指向模板

问题描述

我是编码新手。

我有这段代码,每次完成与给定用户相关的活动时都会生成通知。

目前,通知被生成并存储到一个表中。

我想要的是将通知发送给相关用户。

这是创建通知时发送电子邮件的信号:

def email_notification(sender, instance, created, **kwargs):
""" sends an email notification for notifications """

    user = instance.recipient
    recipients = User.objects.filter(email=user)
    deactivation_link = "link.com"

    if created:
       for recipient in recipients:

           if recipient.email_notification is True:
              mail_helper.send_mail(
                subject="Author's Haven notifictions",
                to_addrs=recipient.email,
                multiple_alternatives=True,
                template_name='notifications.html',
                template_values={
                    'username': recipient.username,
                    'optout_link': deactivation_link,
                    'description': instance.description
                }
            )


post_save.connect(email_notification, sender=Notification)

我用来发送电子邮件的方法是这样的:

def send_mail(self, subject, message=None,
                  to_addrs=[], multiple_alternatives=False, template_name=None,
                  template_values={}):
        """
            Sends an email using the specified addresses

            Parameters:
            ----------
            subject: str
                Email subject
            to_addrs: list
                Email receipients
            message: str
                Email body (for a plain/text email format)
            multiple_alternatives: boolean
                Send email in text/html format
                If false, email is sent as plain text
            template_name: str
                The path to the HTML template to send email in
                Requires multiple alternatives set to True
            template_values: dict
                key: pair items to render in the template message
        """

        if multiple_alternatives:
            template = render_to_string(template_name, template_values)
            body_msg = strip_tags(template)

            email = EmailMultiAlternatives(
                subject, body_msg, self.sender_email, to_addrs)
            email.attach_alternative(template, 'text/html')
            email.send()

        elif not multiple_alternatives:
            _send_mail(subject, message, to_addrs)

这是我的电子邮件模板:

{% extends 'authors/apps/authentication/templates/emailing_template.html' %}
{% block body %}

Here
    <div id="email-verification">
        <p id="greeting">Hey <span id="name">{{ username }}</span>,</p>
        <p id="text-body"><span id="shout">Yaaay!</span> You are now on Authors' Haven.</p>
        <p> Click on the button below to activate your account: </p>
            <div id="activation-button">
               <p>{{ description }}</p>
            </div>
            <div class="failed-button">
                <p>If the button doesn't work, copy the link below and paste it in your browser:
                {{ activation_link }}</p>
            </div>
    </div>
{% endblock %}

最后,这是我的文件夹结构:

authors/apps/notifications/
├── __init__.py
├── models.py
├── __pycache__
│   ├── __init__.cpython-36.pyc
│   ├── renderers.cpython-36.pyc
│   ├── serializers.cpython-36.pyc
│   ├── signals.cpython-36.pyc
│   ├── urls.cpython-36.pyc
│   └── views.cpython-36.pyc
├── renderers.py
├── serializers.py
├── signals.py
├── templates
│   ├── __init__.py
│   └── notifications.html
├── tests
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── test_notifications.cpython-36-PYTEST.pyc
│   └── test_notifications.py
├── urls.py
└── views.py

这会生成通知,但不会将其发送到用户的电子邮件。它引发了错误:

TemplateDoesNotExist at /api/articles/
authors/apps/authentication/templates/emailing_template.html

如何让 django 发送带有模板的电子邮件?

标签: django

解决方案


您已将完整路径放入extends标签中。您只需要放置模板目录中的路径。所以它应该只是{% extends "emailing_template.html" %}

请注意,您不应该将您的应用程序目录放在模板 DIRS 设置中。那是针对主要项目级模板目录的。您应该删除这些值。


推荐阅读