我有這樣的代碼:如何獲得我們的意見的詳細列表
def comment_details(request, comment_id):
comments = Comment.objects.all().filter(id=comment_id)
context = {
'comments': comments,
}
return render(request, 'theme/comment_detail.html', context)
它應該得到評論的id
,並在單獨.html
寫這篇評論。如果我從預期參數中刪除comment_id
,並從filter(id=comment_id)
中刪除並寫入id=24
,它將顯示24th
評論。
請詢問需要什麼,對HTML如下:
{% block container %}
{% for comment in comments %}
<div class="container">
<p>{{ comment.comment}}</p>
<p>By: {{ comment.comment_by }}</p>
<p>Published: {{ comment.comment_datetime }}</p>
{% endfor %}
{% endblock %}
及按鈕應使其打印在這裏:
<td><a href="{% url 'comment_details' %}" details-id="{{comment.id}}"> More details</a></td>
嘗試將其轉換爲int,然後將其傳遞給過濾器參數:'Comment.objects.all()。filter(id = int(comment_id))'如果您說這適用於您'Comment.objects.all ).filter(id = 24)' –
我不知道我明白你想做什麼。你需要一個Comment(爲什麼filter?comment = Comment.objects.get(pk = comment_id))的實例,然後循環遍歷單個實例。如果問題仍然存在,我可能會漏掉一些東西,請多解釋一下。 –
我有一個包含評論的頁面。對於每條評論,我都有一個鏈接,該鏈接應該會導致該單一評論的詳細信息頁面。如果我輸入一個像「(Comment.objects.all()。filter(id = 24)」這樣的隨機id,那麼所有的鏈接都是關於評論24.所以我想如果我能得到「comment_id」,我可以放它在「(Comment.objects.all().filter(id = comment_id)」中,但我不明白我是怎麼做到的,就像我不知道評論如何得到id = 2等等 – random