Django 模板显示原始 HTML 而不是渲染

发布于 2025-01-11 12:05:31 字数 3634 浏览 0 评论 0原文

我正在尝试测试我的 Django 帐户验证电子邮件的电子邮件模板。

但是每次我发送电子邮件而不是看到使用 bootstrap 正确呈现的 html 页面时,电子邮件都会以纯文本形式显示 HTML 代码,我该怎么解决这个问题呢?

acc_verification_email.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
    <div class="col d-flex justify-content-center w-40 h-100 bg-primary">
        <div class="row">
            Learning-Electronics
        </div>

        <div class="row">
            <div class="col">
                <div class="row">
                    Obrigado por te registares!
                </div>
                
                <div class="row">
                    Verifica o teu endereço de E-mail
                </div>
            </div>
        </div>

        <div class="row">
            <p><h2>Olá {% autoescape off %} {{user.first_name}} {% endautoescape %}</h2></p>
            <p>Estás quase pronto para começar! Clica no botão abaixo para verificar o teu e-mail e desfrutares da nossa plataforma de aprendizagem</p>
        </div>

        <div class="row">
            {% autoescape off %} 
            <button type="button" class="btn btn-warning" href="http://{{ domain }}{% url 'account:activate' uidb64=uid token=token %}">VERFICA O EMAIL</button>
            {% endautoescape %}
        </div>
        
        <div class="row">
            <p><h3>Obrigado,</h3></p>
            <p><h3>Da Equipa Learning-Electronics</h3></p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

视图.py

... (some code)
current_site = get_current_site(request)  
mail_subject = 'Ativação de conta no Learning-Electronics'  
message = render_to_string('acc_active_email.html', {  
    'user': account,  
    'domain': current_site.domain,  
    'uid': urlsafe_base64_encode(force_bytes(account.pk)),  
    'token': account_activation_token.make_token(account),  
})

to_email = account.email
email = EmailMessage(mail_subject, message, to=[to_email])  
email.send()
... (some code)

I'm trying to test an email template for my Django Account Verification Email.

But everytime I send the email instead of seeing the html page rendered properly with bootstrap the Email shows the HTML Code in plaintext, what can I do fix this?

acc_verification_email.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
    <div class="col d-flex justify-content-center w-40 h-100 bg-primary">
        <div class="row">
            Learning-Electronics
        </div>

        <div class="row">
            <div class="col">
                <div class="row">
                    Obrigado por te registares!
                </div>
                
                <div class="row">
                    Verifica o teu endereço de E-mail
                </div>
            </div>
        </div>

        <div class="row">
            <p><h2>Olá {% autoescape off %} {{user.first_name}} {% endautoescape %}</h2></p>
            <p>Estás quase pronto para começar! Clica no botão abaixo para verificar o teu e-mail e desfrutares da nossa plataforma de aprendizagem</p>
        </div>

        <div class="row">
            {% autoescape off %} 
            <button type="button" class="btn btn-warning" href="http://{{ domain }}{% url 'account:activate' uidb64=uid token=token %}">VERFICA O EMAIL</button>
            {% endautoescape %}
        </div>
        
        <div class="row">
            <p><h3>Obrigado,</h3></p>
            <p><h3>Da Equipa Learning-Electronics</h3></p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

views.py

... (some code)
current_site = get_current_site(request)  
mail_subject = 'Ativação de conta no Learning-Electronics'  
message = render_to_string('acc_active_email.html', {  
    'user': account,  
    'domain': current_site.domain,  
    'uid': urlsafe_base64_encode(force_bytes(account.pk)),  
    'token': account_activation_token.make_token(account),  
})

to_email = account.email
email = EmailMessage(mail_subject, message, to=[to_email])  
email.send()
... (some code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-18 12:05:31
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'Subject',settings.EMAIL_HOST_USER, to_address_email
text_content = 'This is an important message.'
html_content = '<div>Your have to right all your content inside in this div even styles should include in this</div>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'Subject',settings.EMAIL_HOST_USER, to_address_email
text_content = 'This is an important message.'
html_content = '<div>Your have to right all your content inside in this div even styles should include in this</div>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文