Home > OS >  Multiple Dictionary Output
Multiple Dictionary Output

Time:10-07

There is a section that is running on my flask server, and the HTML file itself. I'm trying to create a sort of results area, where it will display various parts of the dictionary. The code is working, but I'm only able to show a single result instead of multiple results. Any help to show the rest of the dictionary results on the page, instead of a single one will be helpful. Thanks. (I didn't show the full code on my last question)

@app.route('/results')
 def results():
        parameters = {
            "product_type": "blush"
        }
        response = requests. Get(API, json=parameters)
        # print(response.status_code)
        response.raise_for_status()
        data = response.json()
        for item in data:
            brand=item["brand"],
            name = item["name"],
            price = item["price"],
            description = item['description'],
            product_link = item["product_link"],
            website_link = item["website_link"],
            tags = item["tag_list"]
    
            return render_template('results.html',brand=brand, name=name, price=price, description=description, product_link=product_link,
                               website_link=website_link, tags=tags)
    
    
    
    {% include 'header.html' %}
    
    
    <body>
        <section id="results">
                <h1 >Search Results</h1>
                <hr>
                <div >
                    <p ><strong>Brand: </strong>{{ brand }}</p>
                    <p ><strong>Name: </strong>{{ name }}</p>
                    <p ><strong>Product Price: </strong>
                        {% if price %}
                        ${{ price.split('.')[0] }}
                        {% else %}
                            Not available
                        {% endif %}
                    </p>
                    <p ><strong>Product Description: </strong>{{ description }}</p>
                    <p ><strong>Product Link: </strong><a href="{{ product_link }}">{{ product_link }}</a></p>
                    <p ><strong>Website Link: </strong><a href="{{ website_link }}">{{ website_link }}</a></p>
                    <p ><strong>Relevant Tags: </strong>
                        {% if tags %}
                            {% for tag in tags %}
                                {{ tag }},
                             {% endfor %}
                        {% else %}
                            No Tags
                        {% endif %}
                    </p>
                </div>
        </section>
    </body>

CodePudding user response:

Your "return" statement is in the for loop, so it will terminate after the first interaction.

You can run the for loop in the template and pass the list to the render_template

CodePudding user response:

Send data to template and then loop through each item in data, using jinja

@app.route('/results')
 def results():
        parameters = {
            "product_type": "blush"
        }
        response = requests. Get(API, json=parameters)
        # print(response.status_code)
        response.raise_for_status()
        data = response.json()
    
        return render_template('results.html', data=data)



{% include 'header.html' %}


<body>
    <section id="results">
            <h1 >Search Results</h1>
            <hr>
            <div >
                {% for item in data %}

                    <p ><strong>Brand: </strong>{{ item["brand"] }}</p>
                    <p ><strong>Name: </strong>{{ item["name"] }}</p>
                    <p ><strong>Product Price: </strong>
                        {% if item["price"] %}
                            ${{ item["price"].split('.')[0] }}
                        {% else %}
                            Not available
                        {% endif %}
                    </p>
                    <p ><strong>Product Description: </strong>{{ item['description'] }}</p>
                    <p ><strong>Product Link: </strong><a href="{{ product_link }}">{{ item["product_link"] }}</a></p>
                    <p ><strong>Website Link: </strong><a href="{{ website_link }}">{{ item["website_link"] }}</a></p>
                    <p ><strong>Relevant Tags: </strong>
                        {% if item["tag_list"] %}
                            {% for tag in item["tag_list"] %}
                                {{ tag }},
                            {% endfor %}
                        {% else %}
                            No Tags
                        {% endif %}
                    </p>

                {% endfor %}

                

            </div>
    </section>
</body>
  • Related