Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
21040 Lượt xem

Is there a way to print key and value from a python dictionary in loop Qweb?
 For example, if I have a dictionary:
My Code:
<select>
<t t-foreach="[ {key:'1', value:'One'},   {key:'2', value:'Two'}]" t-as="j">
        <option  t-attf-value="j.key"><t t-esc="j.value"/></option>

</t></select>


If i exceute this j.value dict error.

How to print key and value ?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

In Odoo QWeb Template, you don't need to write any extra for the keys, but if you want to write the values, you need to add '_value' right after the alias variable (in your case, the 'j' letter).




(credit goes to 'ifixthat' from St*ckOverfl*w)

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

\https://www.youtube.com/watch?v=cPozFRSZRA0&list=UUZVWw0iLx0pFTzUzvl_XpcA&index=3

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi Silvia,

To print the key and its value you can try this,

<t t-foreach="course_details" t-as="course">
<t t-esc="course.name"/>
<t t-esc="course.id"/>
</t>
Ảnh đại diện
Huỷ bỏ

How to create dictionary?

<t t-set="cont" t-value="dict()"/>

<t t-foreach="cont_obj" t-as="ob">

<t t-set="cont" t-value="cont.update({ob.id: ob.name})"/>

</t>

This code is not working. Would you please tell me how can I create a dictionary in qweb.

Hi Abdul,

To create dictionary follow below code

<t t-set="count" t-value="dict()"/>

<t t-foreach="count_obj" t-as="ob">

<t t-set="count" t-esc="count.update({ob.id: ob.name})"/>

</t>

I hope this code help you

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 21
4879
2
thg 10 20
12960
0
thg 5 17
2459
0
thg 5 22
2170
1
thg 4 22
9401