简体   繁体   中英

Returning Python Dictionary on multiple line in HTML with Flask

I am having a problem returning a Dictionary onto multiple lines in python. The dictionary has the name of a show as its key and then its synopsis as a value, and I would like to format it as such that the Title of the show appears on one line, and then the synopsis on the next.

    @app.route('/rec/', methods=['GET','POST'])
def rec():
    if request.method == "POST":
        animes = openAnimes()
        usr_input = request.form['animeChoice']
        amnt = request.form['howMany']

        try:
            whatisearched = whatwelike(animes, usr_input)['Title']

        except :
            return render_template('rec.html')
            
        try:
            yourDic = run(usr_input, amnt)
            return render_template("result.html", title = whatisearched, rec = yourDic)
        except :
            return render_template('rec.html')

The usr_input gets the show the user inputs, and amnt is how many the program returns. yourDic what the python function on the backend returns, which is a dictionary with the show(key) and its synopsis(value). This is the HTML:

{% extends "rec.html" %}

{% block title %}Recommendation Page{% endblock %}

{% block content %}
<body>
    <h1>If you liked <span style="color: black">{{title}}</span> you should watch:</h1>
    <br>
    <br>

    {{rec}}

    
</body>


{% endblock %}

{{rec}} is what I'd like to return on multiple lines.

Thank you very much.

Edit: I have found a solution although it doesn't let me use a user input variable as a value for my range in the for loop, this is my code.

<h1 style='margin-left:10px'>If you liked <span style="color: black">{{title}}</span> you should watch:</h1>
    <br>
    <br>
    <ol style=' margin-left:10px; color:black'>
    {%for i in range(0, 5)%} 
        <li style = 'color:black; font-size:26px; margin-left:20px'><a href="{{rec[i]['Link']}}" style='text-decoration:none'>{{rec[i]['Title']}}</a></li>
        <br>
        <p style='font-size:18px'>{{rec[i]['Synopsis']}}</p>
        <br>
        
    {%endfor%}
    </ol>

Create a HTML table or definition list or just use a break tag and access the content of rec via rec["show"] and rec["synopsis"] seperately.

Also see Rendering a python dict in Jinja2 / Werkzeug

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