Issue
I am trying to create a table that contains all of the records found inside of a query but only when the said query returns records.
{% if l > 0 %}
{% for record in precord %}
<tr>
<td colspan=1>{{ record['date'] }}</td>
<td colspan=2>{{ record['tasknote'] }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan=3><center>No previous contacts. Get working!</center></td>
</tr>
{% endif %}
l
is equal to the amount of records found. I have tested this by calling l
inside of the if
loop and it does/doesn't fire based upon the correct amount of records.
My issue is I am not able to get the table row to print. It works perfectly fine without the count test, but as soon as I add the wrapper it doesn't. I feel like I am missing a syntax error, but I've looked through the documentation and can't seem to find out what is going on.
Edited to add code that renders template:
@app.route('/view/<id>', methods=["GET", "POST"])
@login_required
def view(id):
form = CreatePR()
record = people.find_one({"_id": ObjectId(id)})
precord = history.find({"parentRecord": str(record['_id'])}).sort('date',pymongo.ASCENDING)
listl = len(list(precord))
if request.method == "GET":
return render_template('view.html', form=form, people=record, precord=precord, auth=current_user.is_authenticated, id=id, l=listl)
elif request.method == "POST":
date = request.form.get('date')
tasknote = request.form.get('tasknote')
adate = datetime.today()
history.insert_one({"date": date, "tasknote": tasknote, "dateAdded": adate.strftime("%x"), "parentRecord": id})
flash('Your record has been added successfully.')
return redirect(url_for('view', id=id))
Solution
There is already a specific construct for this type of case: Jinja does have a for ... else ...
implementation:
{% for record in precord | default([], true) %}
<tr>
<td colspan="1">{{ record.date }}</td>
<td colspan="2">{{ record.tasknote }}</td>
</tr>
{% else %}
<tr>
<td colspan="3">
<center>No previous contacts. Get working!</center>
</td>
</tr>
{% endfor %}
Answered By - β.εηοιτ.βε
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.