I have a json coming from a view in below format ,
[
{
"model":"booking.bookeditem",
"pk":192,
"fields":{
"Booking_id":155,
"hoarding":9,
"date_from":"2017-11-21",
"date_until":"2017-12-06",
"price_net":"34500",
"created":"2017-11-07T11:35:49.675Z"
}
}
]
I need to iterate through this json and print it in the template.Actually, I want to create an invoice email for the user after booking is performed for that I passed bookeditems
as context to email templates.
Here is the view to create booking,
views.py :
def create_order(request,checkout):
booking = checkout.create_order()
if not booking:
return None, redirect('cart:index')
checkout.clear_storage()
checkout.cart.clear()
bookingitems = BookedItem.objects.filter(Booking_id=booking.pk)
booking.send_invoice_email(booking,user,bookingitems)
return booking, redirect('home')
Function to send invoice email,
def send_invoice_email(self,booking,user,bookingitems):
customer_email = self.get_user_current_email()
data = serializers.serialize('json',bookingitems)
subject ="invoice"
ctx = {
'hoardings':data
}
send_invoice.delay(customer_email, subject, ctx)
and i'm using celery&django EmailMessage
to sending invoice email.
task.py:
@shared_task(name="task.send_invoice")
def send_invoice(customer_email, subject, ctx):
to=[customer_email]
from_email = 'example@gmail.com'
message = get_template('email/invoice_email.html').render(ctx)
msg = EmailMessage(subject,message,to=to,from_email=from_email)
msg.content_subtype = 'html'
msg.send()
I tried this :
{% for i in hoardings %}
<tr>
<td>{{ i.pk }}</td>
</tr>
{% endfor %}
But it is not working , and the loop are iterating for each and every string.
Where am i going wrong in the iteration? Please help..