我有一个json来自以下格式的视图,
[
{
"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"
}
}
]
我需要遍历此json并将其打印在模板中。实际上,我想在执行预订后为用户创建发票电子邮件,因为我将bookeditems
作为上下文传递给电子邮件模板。
以下是创建预订的视图,
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')
发送发票电子邮件的功能
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)
我正在使用celery&django EmailMessage
发送发票电子邮件。
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()
我试过了:
{% for i in hoardings %}
<tr>
<td>{{ i.pk }}</td>
</tr>
{% endfor %}
但它不起作用,循环正在为每个字符串迭代。
我在迭代中哪里出错了?请帮忙..
答案 0 :(得分:0)
您已获得list
个JSON对象,因此当您对其进行迭代时,您会在dict
变量中获得i
。要访问列表中的数据,您必须使用
{% for i in hoardings %}
{% for k, v in i.items() %}
<tr>
<td>{{ k.pk }}</td> #or just v if you need only values displayed in template
</tr>
{% endfor %}
{% endfor %}
答案 1 :(得分:0)
为什么要将它转换为JSON?如果要在模板中使用它,只需将其作为字典传递:
{% for i in hoardings %}
<tr>
<td>{{ i.pk }}</td>
<td>{{ i.title }}</td>
</tr>
{% endfor %}
在你的模板中:
string
我认为不需要先将其转换为JSON。这就是你遇到的问题。它以JSON
的形式传递给模板,因此您无法将其作为字典循环。如果你想这样做,你需要找到一个模板标签或过滤器,以便将private static void findTheRepeatedChar(char[] characters) {
/*
* logic: char are inserted as keys and their count as values. If map
* contains the char already then increase the value by 1
*/
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (Character character : characters) {
if (map.containsKey(character)) {
map.put(character, map.get(character) + 1);
} else {
map.put(character, 1);
}
}
// Obtaining set of keys
Set<Character> keys = map.keySet();
/*
* Display count of chars if it is greater than 1. All duplicate chars
* would be having value greater than 1.
*/
for (Character ch : keys) {
if (map.get(ch) > 1) {
System.out.println("Char " + ch + " " + map.get(ch));
}
}
}
转换为字典,这样你就可以像普通字典一样循环它。
答案 2 :(得分:0)
将bookingitems
序列化为OrderDict
项列表,而不是JSON
字符串。
作为普通的Python对象,任务很容易被挑选并放在任务队列中。同样,在此格式中,消息代理能够将此任务传递给工作者。
def send_invoice_email(self, booking, user, bookingitems):
customer_email = self.get_user_current_email()
data = serializers.serialize('python', bookingitems)
subject = 'invoice'
ctx = {
'hoardings': data
}
send_invoice.delay(customer_email, subject, ctx)
在电子邮件模板中,循环显示OrderDict
。
{% for queryset in hoardings %}
<tr>
<td>{{ queryset.pk }}</td>
</tr>
{% endfor %}