我已經對這個問題進行了足夠的搜索,但一直無法找到解決方案。
請幫我。
包含評論串列的板詳細視圖和評論模板均已匯入。我盡可能詳細地介紹了它,所以我希望你給我一個方法。
查看.py
class comment_delete(DeleteView):
model = Comment
success_url = reverse_lazy('board_list.html')
class board_detail(generic.DetailView):
def get(self, request, *args, **kwargs):
pk = self.kwargs['pk']
rsDetail = Board.objects.filter(b_no=pk)
rsData = Board.objects.get(b_no=pk)
rsData.b_count = 1
rsData.save()
comment_list = Comment.objects.filter(Board_id=pk).order_by('-id')
return render(request, "board_detail.html", {
'rsDetail': rsDetail,
'comment_list' : comment_list
})
def post(self, request, *args, **kwargs):
bno = get_object_or_404(Board, b_no=self.kwargs['pk'])
cnote = request.POST.get('c_note')
cwriter = request.POST.get('c_writer')
rows = Comment.objects.create(
Board = bno,
c_note = cnote,
c_writer = cwriter
)
return redirect(reverse('board_detail', kwargs={'pk': self.kwargs['pk']}))
網址.py
path('', views.home, name="home"),
path('board/', views.board.as_view(), name="board"),
path('board/<int:pk>/', views.board_detail.as_view(), name="board_detail"),
path('board_write/', views.board_write, name="board_write"),
path('board_insert', views.board_insert.as_view(), name="board_insert"),
path('board_edit/', views.board_edit, name="board_edit"),
path('board_update/', views.board_update, name="board_update"),
path('board_delete/', views.board_delete, name="board_delete"),
path('board/comment/update/', views.comment_update, name="comment_update"),
path('board/comment/<int:pk>/delete/', views.comment_delete.as_view(), name="comment_delete")
評論.html
{% if not comment_list %}
<p class="text-center"> empty </p>
{% endif %}
<div style="margin:20px 0; margin-right:400px; margin-left:400px;">
{% for i in comment_list %}
<table width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr style="background-color:#f1eee3;height:45px;border-top: 1px solid #333;">
<td style="padding-left: 10px;border-top: 1px solid #eee;width: 114px;" align="center" width="20%">{{ i.c_writer }}</td>
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;" align="left" width="70%">{{ i.c_note }}
<span style="color: #999;font-size: 11px;display: block;">{{ i.c_date }}</span>
</td>
{% if user.username == i.b_writer or user.is_staff %}
<form action="{% url 'comment_update' %}">
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;" align="right" width="10%">
<button class="btn btn-outline-success my-2 my-sm-0">edit</button>
</td>
</form>
<form action="{% url 'comment_delete' pk=i.Board_id %}" method='POST'>
{% comment %} <form action="{% url 'comment_delete' Board_id=i.Board_id id=i.id %}" method='POST'> {% endcomment %}
{% csrf_token %}
<td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;padding-right: 5px;" align="right" width="10%">
<button class="btn btn-outline-success my-2 my-sm-0" style="margin-right:10px;">delete</button>
</td>
</form>
{% endif %}
</tr>
</tbody>
</table>
{% endfor %}
{% 包含 'comment_write.html' %}
uj5u.com熱心網友回復:
除非另有說明,洗掉視圖除了要洗掉的物件中的 pk 之外,在您的情況下,這將是注釋:
path('board/comment/<int:pk>/delete/', views.comment_delete.as_view(), name="comment_delete")
此外,您沒有使用評論 ID 而是使用板 ID,這當然是不同的,并且會導致您的 url 指向不正確的物件。
相同的邏輯適用于DetailView、UpdateView、DeleteView,這些視圖需要您在這些視圖中的模型屬性中參考的主鍵(或 ID)。
最后一件事,什么是i.Board_id和id.id?當使用最明確的變數時,也要以小寫命名變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381161.html
標籤:姜戈
