Home > Mobile >  404 error : no comment matches the given query
404 error : no comment matches the given query

Time:12-15

I've searched enough about this problem, but haven't been able to find a solution.

Please help me.

The board detail view and comment template containing the comment list were all imported. I brought it in as much detail as possible, so I hope you give me a way.

View.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']}))

urls.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")

comment.html

{% if not comment_list %}
<p > 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 >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  style="margin-right:10px;">delete</button>
                </td>
            </form>
            {% endif %}
        </tr>
    </tbody>
</table>
{% endfor %}
{% include 'comment_write.html' %}

CodePudding user response:

Unless specified otherwise the delete view except only the pk from the object you want to delete so in your case that would be the comment :

path('board/comment/<int:pk>/delete/', views.comment_delete.as_view(), name="comment_delete")

Also you were not using the comment ID but the board ID, which of course is different and will cause your url to point the incorrect object.

The same logic applies to the DetailView, UpdateView, DeleteView, those views expects the primary key (or ID) that you reference in the model attribute within those views.

Last thing, what is i.Board_id and id.id ? When using variable be the most explicit, do name your variables in lower case as well.

  • Related