I am trying to print who a friend request has come from. To do so I have this app.route:
@app.route("/friend_requests", methods=["GET", "POST"])
def friend_requests():
user = session["user"] or None
find_request = mongo.db.friend_requests.find_one({"friend_request_to": user})
print(find_request)
return render_template("friend_requests.html", find_request=find_request)
I am then adding this to a for loop in HTML using Jinja:
{% for find_requests in find_request %}
<span>from {{ find_request.friend_request_from }}</span>
{% endfor %}
The print(find_request) output in the terminal is this:
{'_id': ObjectId('600d281ec3957da9d448b845'), 'friend_request_from': 'admin2', 'friend_request_to': 'test3'}
And the output into the HTML (I will flash it up later when I get this right) is this:
from admin2 from admin2 from admin2
The loop seems to repeat for every item in the collection.
What I really want is the loop to find every time a user friend request is sent to this logged in user. In this case, for testing purposes, only one request has been sent.
How do I get the loop to print just the friend_request_from item in the collection?
question from:
https://stackoverflow.com/questions/65871793/for-loop-using-jinja-and-pymongo-repeating-the-same-output-for-an-entire-dict 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…