简体   繁体   中英

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. 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.

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. In my result.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 ([]). 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. This should look like below

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

template.html

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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