2

Having this for Flask:

example = mongo.db.example
got_name = example.find({'name':1})
got_lastname = example.find({'lastname':1})

details = {'name' : got_name, 'lastname' : got_lastname}

return render_template('blabla.html', details=details)

Then the for loop using Jinja in my HTML (wanting it to be a table):

{% for x in details}
<tr>
    <td>{{ x.['name'] }}</td>
    <td>{{ x.['lastname'] }}</td>
</tr>
{% endfor %}

But it won't work, it doesn't display anything in my table. I wrote this example above now, but my code is similar.

2 Answers 2

2

You may want to use find_one() instead of find() which returns a cursor to the documents which match the criteria. find_one() returns a single document which can then be used in the dictionary, instead of a cursor:

example = mongo.db.example
doc = example.find_one()

details = { 'name' : doc['name'], 'lastname' : doc['lastname'] }

return render_template('blabla.html', details=details)

Or

example = mongo.db.example
details = example.find_one({}, {'name':1, 'lastname':1})

return render_template('blabla.html', details=details)

And your template will be

<tr>
    <td>{{ details['name'] }}</td>
    <td>{{ details['lastname'] }}</td>
</tr>

If you want to iterate the whole collection and return a list if documents with just the name and lastname fields, then you should use the find() method. If you have a relatively small dataset, the following code will convert the entire result set (Cursor) into a list (everything is pulled into memory):

example = mongo.db.example
details = list(example.find({}, {'name': 1, 'lastname': 1}))

return render_template('blabla.html', details=details)

Then iterate the list in your template

{% for doc in details}
<tr>
    <td>{{ doc['name'] }}</td>
    <td>{{ doc['lastname'] }}</td>
</tr>
{% endfor %}
Sign up to request clarification or add additional context in comments.

5 Comments

Everything works fine, but then I tried to iterate using your method. I've details = list(bans.find({}, {'Nume codat' : 1, 'SteamID' : 1, 'Motiv' : 1})), then <td>{{ x.['Nume codat'] }}</td> but I get „TemplateSyntaxError: expected name or number” @ <td>{{ x.['Nume codat'] }}</td>. Thanks for the reply, though!
Weird, works if I delete the dot, ex. {{ x.['bla'] }} was wrong, but {{ x['blabla'] }} works. Should it work or was something done wrong by your side?
My bad, that was a typo on your part I failed to correct in giving my answer. I have updated the answer to reflect the correct approach.
One more question: how do I make it so the first display is actually the last data that was inserted into the database? Cause if I insert some data now, it'll actually display at the bottom, after the other data. I want it to be inversed, hope you catch my drift.
You can try sort on the _id key (if your documents' _id key is an ObjectId) as details = list(example.find({}, {'name': 1, 'lastname': 1}).sort('_id', -1))
0

Your template would actually have to be more like this:

<tr>
    <td>{{ details['name'] }}</td>
    <td>{{ details['lastname'] }}</td>
</tr>

No loops.

If you had an array of dictionaries, then you could use your initial template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.