小编Rob*_*ers的帖子

Django - 如何遍历字典列表以连接同一元素的值

我有一个字典列表如下:

listDict = [{'product':'sandwich','price':'5200'}, {'product':'hamburger','price':'3000'}]
Run Code Online (Sandbox Code Playgroud)

迭代我做的元素:

{%for element in listDict%}
    {% for key,value in element.items %}
        <input type="checkbox" name = "bar" value = "{{ value }}">{{ value }}<br>
    {% endfor %}
{% endfor %}                    
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,这将打印:

sandwich

5200

hamburger

3000
Run Code Online (Sandbox Code Playgroud)

但我怎么能连接这些值才能打印出这样的东西:

sandwich - 5200

hamburger - 3000
Run Code Online (Sandbox Code Playgroud)

我不能做以下的事情:

for element in listDict:
    element['product']+" - "+element['price']
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python django

8
推荐指数
1
解决办法
1万
查看次数

Python基础知识:如何在方程式中设置两个函数参数?

我有一个关于python函数和参数的基本问题

鉴于此功能:

def findArticleWithAttr(tableattrib, user_url_input):
    articles = Something.objects.filter(tableattrib=user_url_input) 
Run Code Online (Sandbox Code Playgroud)

我用以下函数调用该函数:

findArticleWithAttr(attribute1, userinput1)
Run Code Online (Sandbox Code Playgroud)

tableattrib不是由findArticleWithAttr属性1设置的,只是接受后者(userinput1)参数并替换它.

我怎样才能让python在等式中设置两个参数?

python function

2
推荐指数
1
解决办法
215
查看次数

标签 统计

python ×2

django ×1

function ×1