-1

I am using this code, which (most of the times, because it is different everytime) prints multiple lines of strings.

if request.method == "POST":
    d = {'Dirt 4': g_dirt4, 'Destiny 2': g_destiny2, 'South Park: The Fractured but Whole': g_southpark, 'Call of Duty: WWII': g_codww2, 'Star Wars Battlefront II': g_bfront2, 'Red Dead Redemption 2': g_reddead2, 'FIFA 18': g_fifa18, 'MotoGP™17': g_motogp17, 'The Elder Scrolls Online: Morrowind': g_elderscrolls, 'Crash Bandicoot N. Sane Trilogy': g_crashbandicoot}

    max_value = max(d.values())
    maximal_keys = [ k for k,v in d.items() if v==max_value ]
    for title in maximal_keys:
        print (title)

    return render_template("result_page.html", title=title)

So this means, when you click on a button, it will return the result_page.html and in the html code, the title variable will be replaced with the actual title. So this is the html code:

<body>
    This game(s) belongs to you: {{ title }}
</body>

When the python code prints something it looks like this, but like I said it is different sometimes:

2017-06-14 07:08:22 Destiny 2
2017-06-14 07:08:22 Call of Duty: WWII

The problem is that when I click the button in this case, it will only change the title to Destiny 2. I won't get multiple lines of titles like I want. How do I do that?

So now in this case it will show me:

https://i.gyazo.com/30c4afc6434c811b958804aad505137e.png

But I would like to see this:

https://i.gyazo.com/ee523d9d1dd6a28ba095e4fcfd8203ab.png

Jip Harthoorn
  • 292
  • 1
  • 8
  • 25

3 Answers3

1

Your current html template does not really support printing multiple elements, and your code also returns only one title to render.

You should check this other question, which seems similar to your problem : How to build up a HTML table with a simple for loop in Jinja2?

You should end up with something like this :

<body>
This game(s) belongs to you:
    <ul>
    {% for title in titles %}
        <li>{{title}}</li>
    {% endfor %}
    </ul>
</body>
Anthony Rossi
  • 1,182
  • 8
  • 14
  • I tried this, max_value = max(d.values()) maximal_keys = [ k for k,v in d.items() if v==max_value ] for title in maximal_keys: print (title) an_title = dict(title) tiles.append(an_title) return render_template("result_page.html", titles=titles) but it doesn't work. I have no idea what I should do :/ – Jip Harthoorn Jun 14 '17 at 13:57
  • You will in all case need to adapt your html template to work with a list. I also noticed that your return block is not a child of your logic block, thus the variables that you declare in your if block are not visible by the return statement. If you declared these before you are fine – Anthony Rossi Jun 14 '17 at 14:02
  • Oh sorry it was a child already but that was just a little mistake in the thread. I know that I have to adapt the html to a list but I just dont know how. – Jip Harthoorn Jun 14 '17 at 14:05
  • Edited to add jinja template – Anthony Rossi Jun 14 '17 at 14:14
0

You want your template "to iterate over something"

In your template the code has to look like this:

<body>
{% for t in list_of_titles %}
 <p>- {{ t }}</p>
{% endfor %}
</body>

and you have to pass something to iterate over in your route function, e.g. a list of titles.

return render_template('result_page.html', list_of_titles=list_of_titles)

Q: Do you want to display ALL of the titles at once or do you want to add one title after pressing your button?

edit after comment:

Your list_of_titles has to look like

list_of_titles = ['MotoGP', 'Dirt']
bnrc_
  • 63
  • 2
  • 9
  • Anthony did a better job while i was working out my answer – bnrc_ Jun 14 '17 at 14:29
  • I would like to show all titles at one. Each at one line. Now with this code it gives me this, while title = MotoGP17 and Dirt4 https://i.gyazo.com/f4d853ecd261aeb60e1587bae7baaa64.png – Jip Harthoorn Jun 14 '17 at 14:29
  • you are passing a string not a list, but you want to pass a list – bnrc_ Jun 14 '17 at 14:36
0

Thanks to Anup, the question is answerd:

return render_template("result_page.html", title=maximal_keys)

and

<ul> {% for n in title %} <li>{{n}}</li> {% endfor %} </ul> 

Which gives me this:

https://i.gyazo.com/2e41a4ea8d852dc4f14a7aa02979bd2d.png

@Update as per comment:

     <li>
        <div class=”text”>
           <h1>{{ n }}</h1>
        </div>
        <div class=”image”>
           <img src=”{{ img_path }}”>
        </div>            
     </li>
Anup
  • 200
  • 1
  • 13
Jip Harthoorn
  • 292
  • 1
  • 8
  • 25