Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印Django中Python函数创建的列表_Python_Html_Django_Function - Fatal编程技术网

打印Django中Python函数创建的列表

打印Django中Python函数创建的列表,python,html,django,function,Python,Html,Django,Function,我想获取由以下函数(计数器列表)创建的列表,并将其打印在HTML Django模板上。我知道在HTML中我需要 {% for i in listofcounters %} <p> {{ something here maybe? }} </p> {% endfor %} 如果我没有弄错的话,您所要做的就是将一个dict传递给模板。 我还没有试过你的代码,但从一个快速的一瞥,这是我得到的 from mtgsdk import Card def findcounters(

我想获取由以下函数(计数器列表)创建的列表,并将其打印在HTML Django模板上。我知道在HTML中我需要

{% for i in listofcounters %}
<p> {{ something here maybe? }} </p>
{% endfor %}

如果我没有弄错的话,您所要做的就是将一个
dict
传递给模板。 我还没有试过你的代码,但从一个快速的一瞥,这是我得到的

from mtgsdk import Card

def findcounters(request):
    listofcounters = []
    cards_from_set = Card.where(set='iko').all()
    for card in cards_from_set:
        if "counter target" in str(card.text).lower():
            listofcounters.append(card.name)
    listofcounters = list(dict.fromkeys(listofcounters))
    #**********try adding the following code to the code ************
    ctx = { 'listofcounter':listofcounters }

    return render(request, 'yourtemplate.html', ctx}

可以将其包含在传递到视图内部渲染的对象中

从django.exe导入渲染
来自mtgsdk进口卡
def findcounters():
计数器列表=[]
cards\u from\u set=Card.where(set='iko').all()
对于\u集中的\u卡中的卡:
如果str(card.text).lower()中的“计数器目标”:
counters.append列表(card.name)
listofcounters=列表(dict.fromkeys(listofcounters))
返回计数器列表
定义我的视图(请求):
计数器=findcounters()
返回呈现(请求'file.html',{'counters':counters})

然后将此视图导入URL,您就可以开始比赛了

此函数不提供请求,因此没有返回
render()
@VishalSingh的必要。您说得对,我错过了,谢谢。但这仍然使op更接近解决方案。还是有更好的办法?
from mtgsdk import Card

def findcounters(request):
    listofcounters = []
    cards_from_set = Card.where(set='iko').all()
    for card in cards_from_set:
        if "counter target" in str(card.text).lower():
            listofcounters.append(card.name)
    listofcounters = list(dict.fromkeys(listofcounters))
    #**********try adding the following code to the code ************
    ctx = { 'listofcounter':listofcounters }

    return render(request, 'yourtemplate.html', ctx}