【问题标题】:Send_email in django with data from a view as an HTML page在 django 中发送电子邮件,将视图中的数据作为 HTML 页面
【发布时间】:2014-09-11 07:20:22
【问题描述】:

我很难处理这个。我有这样的看法:

@login_required
def verFactura(request, id_factura):
    fact = Factura.objects.get(pk = id_factura)
    cliente = Cliente.objects.get(factura = fact)
    template = 'verfacturas.html'
    iva = fact.importe_sin_iva * 0.21
    total = fact.importe_sin_iva + iva

    extra_context = dict()
    extra_context['fact'] = fact
    extra_context['cliente'] = cliente
    extra_context['iva'] = iva
    extra_context['total'] = total

    return render_to_response(template, extra_context)

从数据库中获取数据并进行一些数学运算并将其显示在这样的模板上:

<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div id="">
                <p id="address">
                    {{fact.nombre_cliente}}
                </p>
                <p id= "numero">
                    {{fact.numero_De_Factura}}
                </p>
                <div id="logo">
                    <img id="image" src="{% static 'img/Home/Logo-Exisoft.png' %}"                 alt="logo" />
                </div>
            </div>

            <div style="clear:both"></div>

            <div id="customer">
                <div id="datos">
                    <p id = "direccion">
                        {{cliente.Direccion}}
                    </p>
                    <br>
                    <p id = "direccion">
                        {{fact.RI}}
                    </p>
                </div>
                <table id="meta">
                    <tr>
                        <td class="meta-head">Fecha</td>
                        <td><textarea id="date">{{fact.fecha_factura}}</textarea></td>
                    </tr>
                    <tr>
                        <td class="meta-head">CUIT</td>
                        <td><div class="due">{{cliente.CUIT}}</div></td>
                    </tr>
                </table>
            </div>

            <table id="items">
                <tr>
                    <th class="tipo">Tipo de Factura</th>
                    <th class="descripcion">Descripcion</th>
                    <th>Precio</th>
                </tr>
                <tr class="item-row">
                    <td><div><textarea>{{fact.tipo_Factura}}</textarea></div></td>
                    <td class="description"><textarea>{{fact.descripcion}}</textarea></td>
                    <td><span class="price">$ {{fact.importe_sin_iva}}</span></td>
                </tr>
             </table>
             <table id="totales">
                    <tr>
                        <td class="total-line">Subtotal</td>
                        <td class="total-value"><div id="subtotal">$ {{fact.importe_sin_iva}}</div></td>
                    </tr>
                    <tr>
                        <td class="total-line">Iva</td>
                        <td class="total-value"><div id="total">$ {{iva}}</div></td>
                    </tr>
                    <tr>
                        <td  class="total-line">Precio Pagado</td>
                        <td class="total-value"><textarea id="paid">$ {{total}}</textarea></td>
                    </tr>
              </table>

              <div id="terms"></div>        
        </div><!-- /.box-body -->
    </div><!-- /.box -->
</div>

所以它所做的就是转到视图并呈现字段的信息并完成它们,非常简单。问题是,当用户创建账单(西班牙语中的 Factura)时,我还将 {{iva}} 和 {{total}} 等字段中的数据保存到数据库中,所以我在两个地方有这些信息:数据库和模板。好的。所以我想要做的是发送一封带有正确 HTML 的电子邮件(希望与模板中出现的表格相同)。因为每张账单 (Factura) 的信息都不同,所以我无法发送静态信息并通过电子邮件发送,所以它必须显示每张账单的信息。

那么我该怎么做呢?从数据库中获取信息并更改标签之间显示的值以显示每张账单的正确信息。

提前谢谢你。我真的很感激帮助或任何将我指向正确方向的想法。谢谢你

【问题讨论】:

    标签: python database django django-templates django-views


    【解决方案1】:

    您可以使用内置的 django send_mail 方法。您还将了解如何发送 HTML 电子邮件。

    更好的解决方案是使用django_templated_email 应用程序,该应用程序使用普通的 django 模板,允许您编写丰富的 HTML 电子邮件并将其发送出去。

    它有一个非常简单的 API:

    from templated_email import send_templated_mail
    send_templated_mail(
            template_name='welcome',
            from_email='from@example.com',
            recipient_list=['to@example.com'],
            context={
                'username':request.user.username,
                'full_name':request.user.get_full_name(),
                'signup_date':request.user.date_joined
            },
            # Optional:
            # cc=['cc@example.com'],
            # bcc=['bcc@example.com'],
            # headers={'My-Custom-Header':'Custom Value'},
            # template_prefix="my_emails/",
            # template_suffix="email",
    )
    

    要在您的视图中使用它,请按以下步骤操作:

    1. 安装库pip install django_templated_email
    2. 在您为应用程序拥有views.py 的同一目录中创建一个templates 目录。
    3. 在您创建的templates 目录中,创建另一个名为templated_email 的目录。您的模板电子邮件将放在此处。

    现在,在这个 templates/templated_email 目录中创建一个名为 receipt.email 的文件

    复制并粘贴以下内容:

    {% block subject %}Your Receipt # {{fact.numero_De_Factura}}{% endblock %}
    
    {% block html %}
    <html>
    <head>
    <title>Receipt</title>
    </head>
    <body>
    <table>
      <thead>
        <tr><th>Receipt Number</th><tr><th>Client Number</th></tr>
      </thead>
      <tbody>
        <tr><td>{{fact.numero_De_Factura}}</td><td>{{fact.nombre_cliente}}</td></tr>
      </tbody>
    </table>
    </html>
    {% endblock html %}
    
    {% block plain %}
      Thank you. Your receipt number is: {{fact.numero_De_Factura}} and your client # is {{fact.nombre_cliente}}
    {% endblock plain %}
    

    您可以根据自己的喜好调整 HTML,甚至可以从其他模板继承。确保保留特殊的 {% block html %}{% block subject %},因为这是应用程序创建 HTML 电子邮件及其主题的方式。

    最后,发送电子邮件是最简单的部分。现有视图方法中的几行代码:

    from django.shortcuts import render
    from templated_email import send_templated_mail
    
    @login_required
    def verFactura(request, id_factura):
        fact = Factura.objects.get(pk = id_factura)
        cliente = Cliente.objects.get(factura = fact)
        template = 'verfacturas.html'
        iva = fact.importe_sin_iva * 0.21
        total = fact.importe_sin_iva + iva
    
        extra_context = dict()
        extra_context['fact'] = fact
        extra_context['cliente'] = cliente
        extra_context['iva'] = iva
        extra_context['total'] = total
    
        # Send the email
        send_templated_mail(template_name='receipt',
                            from_email='robot@server.com',
                            recipient_list=[request.user.email],
                            context=extra_context)
    
        return render(request, template, extra_context)
    

    【讨论】:

    • 你好。是的,我只是看着它。一个问题:这将采用模板,其中包含每张账单的数据库中的信息,并制作一封电子邮件?那是我唯一不明白的事情,我的英语不太好,对不起
    • 就像你现在用你的视图做的一样;除了将带有信息的模板发送到浏览器之外,它将通过电子邮件发送。事实上,您可以使用您现在使用的相同模板。
    • 哇,这真的很有帮助。我要试试,让你知道。感谢您的提示
    • 你就是那个男人。但我有两个问题:我可以将这个“send_templated_mail”作为一个函数添加到按钮吗?我就这样保存了receipt.email,它不必以“.html”或“.py”结尾,或者可以以“.email”结尾
    • 模板必须以.email结尾,你可以把函数放在任何你想要的地方,但你必须在return之前调用它,否则不会发送电子邮件。
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2015-06-27
    • 2015-04-22
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多