我的模板中有一個按鈕,只有在
1 用戶已登錄并且;2 用戶是發布串列的人
但即使滿足上述所有條件也不會出現,所以我的模板中有這個:
{% if user.username == "{{ users }}" %}
<form action="listing" method="GET">
<input class="btn btn-primary" type="submit" value="Close Listing" name="close">
</form>
{% endif %}
其中“{{users}}”是發布串列的用戶名,因此如果匹配,該用戶將獲得按鈕
視圖.py
listing = Listing.objects.all().filter(title=title).first()
user = listing.user
將其傳遞給模板:
return render(request, "auctions/listing.html", {
"users": user
})
找不到我做錯了什么
uj5u.com熱心網友回復:
改變你的邏輯;
第一的:
listing = Listing.objects.all().filter(title=title).first()
# Change variable name, you should name your variables with their purpose
author = listing.user
return render(request, "auctions/listing.html", {
"author": author
})
第二:
# No need to use {{ }} in condition because `Jinja` does it for you.
{% if request.user == author %} # {% if request.user.username == author.username %}
<form action="listing" method="GET">
<input class="btn btn-primary" type="submit" value="Close Listing" name="close">
</form>
{% endif %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/532073.html
標籤:django模板
上一篇:編譯時檢查是否存在使用C 20的函式的模板特化需要運算式
下一篇:將字串推回vector<T>
