django中的Send_email,视图中的数据为HTML页面

时间:2014-07-20 19:21:59

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

我很难相处。我有这样的观点:

@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)都是不同的,我不能发送和发送静态信息,它必须显示每个账单的信息。

那我怎么能这样做?从数据库中获取信息并更改标记之间显示的值,以显示每个帐单的正确信息。

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

1 个答案:

答案 0 :(得分: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. 在您的应用程序templates的同一目录中创建一个views.py目录。
  3. 在您创建的templates目录中,创建另一个名为templated_email的目录。您的模板电子邮件将会显示在此处。
  4. 现在,在名为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)