Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以django格式发送电子邮件,其中包含来自HTML页面视图的数据_Python_Database_Django_Django Templates_Django Views - Fatal编程技术网

Python 以django格式发送电子邮件,其中包含来自HTML页面视图的数据

Python 以django格式发送电子邮件,其中包含来自HTML页面视图的数据,python,database,django,django-templates,django-views,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

我很难接受这个。我有这样的看法:

@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>

{{fact.nombre_cliente}}

{{事实数字}

{{cliente.Direccion}


{{fact.RI}}

德国福查 {{fact.fecha_factura}} CUIT {{cliente.CUIT} 蒂波德法图拉酒店 描述 普里西奥 {{fact.tipo_Factura}} {{fact.description}} ${{fact.importe_sin_iva} 小计 ${{fact.importe_sin_iva} 伊娃 ${{iva} 普里西奥帕加多 ${{total}
这样做的目的是转到视图,呈现字段的信息并完成它们,非常简单。问题是,当用户创建一个账单(西班牙语中的Factura)时,我还将{{iva}}和{{total}等字段中的数据保存到数据库中,因此我在两个地方拥有这些信息:数据库和模板。好啊所以我想做的是发送一封带有正确HTML的电子邮件(希望这是模板中显示的相同表格)。因为每个账单(Factura)的信息都不同,我不能用静态信息发送和发送电子邮件,所以它必须显示每个账单的信息

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


先谢谢你。我真的很感谢你的帮助或任何能为我指明正确方向的想法。谢谢

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

更好的解决方案是使用应用程序,它使用普通的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",
)
要在视图中使用它,请执行以下步骤:

  • 安装库
    pip安装django\u模板化\u电子邮件
  • 在应用程序的
    views.py
    目录中创建一个
    templates
    目录
  • 在您创建的
    templates
    目录中,创建另一个名为
    templated\u email
    的目录。您的模板电子邮件将发送到此处
  • 现在,在
    templates/templated_email
    目录中创建一个名为
    receive.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”或“.py”结尾,也不必以“.email”结尾。模板必须以
    .email
    结尾,你可以将该函数放在任何你想要的地方,但你必须在
    返回之前调用它,否则不会发送电子邮件。
    
    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)