Im having a hard time with this. I have a view like this:
@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)
that takes data from the databse and do some math and shows it on a template like this:
<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>
so what this does is go to the view and renders the information for the fields and completes them, pretty simple. The thing is that when the user creates a bill (Factura in spanish) i also save the data from fields like {{iva}} and {{total}} into the database, so i have this information in two places: database and the template. OK. So what i want to do is to send an email with proper HTML (hopefully this same tables that appear in the template). Because this information is different for every bill (Factura) i can´t send and email with static information, it has to show the information of each of the bills.
So how can i do this?. Take the info from the database and change the value that appears between the tags to show the correct information for each bill.
Thank you in advance. I would really appreciate the help or any idea that points me in the right direction. Thank you
You can use the built-in django send_mail
method. You'll see how to send HTML emails as well.
The better solution is to use django_templated_email
app which uses the normal django templates to allow you to compose rich HTML emails that it will send out.
It has a very simple 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",
)
To use it in your view, follow these steps:
pip install django_templated_email
. templates
directory in the same directory that you have views.py
for your application. templates
directory you created, create another directory called templated_email
. Your template emails will go here. Now, create a file in this templates/templated_email
directory called receipt.email
Copy and paste the following into it:
{% 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 %}
You can adjust the HTML to your liking and even inherit from other templates. Make sure you keep the special {% block html %}
and {% block subject %}
as this is how the application will create your HTML email and its subject.
Finally, to send the email is the easiest part. Just a few lines in your existing view method:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.