简体   繁体   English

从Django中的视图在HTML页面上显示列表

[英]Displaying list on HTML page from view in Django

I've got a list in my views.py which I'm sending to a HTML page via the render function and I'm printing it out on that page. 我的views.py中有一个列表,该列表通过render函数发送到HTML页面,并在该页面上打印出来。 Problem is that, while all other lists that I send are getting printed on the HTML page, these 2 lists I'm sending in particular are not. 问题是,虽然我发送的所有其他列表都打印在HTML页面上,但是我要发送的这两个列表却没有。

app/views.py app / views.py

alttrue = []
altfalse = []
alttrue, altfalse = altCheck(soup)
results = { 'alttrue' : alttrue,
            'altfalse' : altfalse,
          }
render(context,'app/result.html', results)

Now altCheck is a function that returns two lists where at least one of them have values, that's for sure. 现在altCheck是一个函数,它返回两个列表,其中至少有一个具有值,这是肯定的。 In my result.html, 在我的result.html中,

results.html results.html

<h5>Alt pass results</h5>
{% for tag in alttrue %}
<p>{{ tag }}</p>
{% endfor %}
<h5>Alt fail results</h5>
{% for tag in altfalse %}
<p>{{ tag }} </p>
{% endfor %}

The problem is that running a for loop through the list and trying to print it, returns an empty list ([]). 问题是在列表中运行一个for循环并尝试打印它会返回一个空列表([])。 However, If I were to try and print it as such 但是,如果我尝试这样打印

<p>{{ alttrue }}</p>
<p>{{ altfalse }}</p>

It works fine. 工作正常。 It prints the list. 它打印列表。 Just, iterating the list and printing out the variables doesn't work. 只是,迭代列表并打印出变量不起作用。

Here is whats going wrong: 这是怎么回事:

alttrue = []
altfalse = []
alttrue, altfalse = altCheck(soup)
results = { 'alttrue' : altpass,
            'altfalse' : altfail,
          }
render(context,'app/result.html', results)

in the result dict the variable in under quotes (key) is suppose to be name of context variable. 在结果dict中,下引号(key)中的变量假定为上下文变量的名称。 This should look like below 如下所示

alttrue = []
altfalse = []
alttrue, altfalse = altCheck(soup)
results = { 'altpass' : alttrue ,
            'altfail' : altfalse ,
          }
render(context,'app/result.html', results)

template.html template.html

{% for x in altpass %}
{{x}}
{% endfor %}

{% for x in altfail %}
{{x}}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM