jinja2 - Display items without brackets/quotes

BrettJ

I'm returning a list to my Flask View but I want to display it without the brackets or single quotes.

My HTML is formatted simply around a bootstrap dropdown

<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Park Names
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      {% for park in parks_found %}
      <a class="dropdown-item" href="#">{{ park }}</a>
      {% endfor %}
    </div>
  </div>

Which outputs the drop down as shown - I would love to display it without the brackets/quotes - Any help is much appreciated.

enter image description here

gtalarico

Looks like each park item is a list of one item and not a string. In other words, parks_found contains a list of lists and not a list of park names as strings.

Solution

You can either make sure parks_found is a list of strings before passing, or you can grab the first item from it. Alternatively, if it's possible that each item inside parks_found actually contains a list, you add another loop for each park item.

View

def view():
    return render_template('index.html',
                           parks_found=[['Park A'], ['Park B']],
                           flat_parks_found=['Park A', 'Park B'])

Html

<h1>Original</h1>
{% for park in parks_found %}
<p>{{ park }}</p>
{% endfor %}

<h1>Grab first item</h1>
{% for park in parks_found %}
<p>{{ park[0] }}</p>
{% endfor %}

<h1>Flattaned list</h1>
{% for park in flat_parks_found %}
<p>{{ park }}</p>
{% endfor %}

HTML Output


Original

['Park A']

['Park B']

Grab first item

Park A

Park B

Flattaned list

Park A

Park B


Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jinja2: format + join the items of a list

Modify list items in Ansible / Jinja2

Formatting a list of items in Ansible/Jinja2

How to display filtered items without the submit button?

Use Jinja2 to compare 2 items within the same list

display list items in 2 columns

Jinja2 looping over list of dictionary items

Jinja2 - Render list items in groups of x

Get nested dict items using Jinja2 in Flask

How to apply jinja2 filter to ansible list items?

Get count of list items that meet a condition with Jinja2

how to fill items into two columns in Jinja2 template

jinja2 error 'list object' has no attribute 'items'

How to display string representation of a context object in Jinja2?

How do you display markdown value using jinja2?

Laravel 5.3 How does bootstrap interpret and display items without internet

WPF Compobox - how to display entire content of the two items without scrolling?

Display RecyclerView items starting from bottom without reversing the order

Why tab bar items only display their titles without icons?

list items display without bullets use text instead?

Xamarin.Forms - Display all items without scrolling

Display number of items from array of objects without duplicate

How can Jinja2 read a function without passing it explicitly?

render jinja2 template without a Flask context

How to reference dictionary value without iterating in Jinja2 template?

Display 2 caroussels without image confusion on display

Avoid to display 2 items at the same time with JQuery

How do I append static items to a dict using Jinja2

How to display a date_time field in jinja2 template? (flask)