You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django工单评论已存数据库但前端无法显示问题求助

Fixing Django Ticket Comment Display Issue

Let's walk through the problems stopping your comments from showing up on the edit ticket page, and fix them one by one:

1. Fix the Comment-Ticket Association Bug

Looking at your CreateCommentView, there's a critical mistake when saving new comments: you're assigning Comment.ticketId = post, but your Comment model uses IdTicket as the foreign key field linking to Ticket (note that ticketId is a UUID field on the Ticket model itself, not the foreign key). This means your comments aren't actually being linked to the correct ticket—so even though they save to the database, the ticket can't find them.

Update the code in CreateCommentView like this:

def CreateCommentView(request, pk):
    post = get_object_or_404(Ticket, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            # Rename variable to avoid clashing with the Comment model
            comment = form.save(commit=False)
            # Use the correct foreign key field name
            comment.IdTicket = post
            comment.save()
            return redirect('viewComment', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'Createcomment.html', {'form': form})

2. Pass Comment Data to the Edit Ticket Template

Your EditTicketView (an UpdateView) doesn't send the ticket's comments to the template. Right now, your template tries to loop over comment_list.all, but comment_list doesn't exist in the context.

You have two easy fixes here:

Option A: Access Comments Directly from the Ticket Object

Since you set related_name='comment' on the Comment model's foreign key, you can fetch all comments for the current ticket directly using object.comment.all in the template. Update your template loop:

{% for comment in object.comment.all %}
<div class="card-body">
    <p>{{ comment.timestamp }}</p>
    <strong>{{ comment.user }}</strong>
    <p>{{ comment.description | linebreaks}}</p>
</div>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}

Pro tip: Add .order_by('-timestamp') to show newest comments first: object.comment.all().order_by('-timestamp')

Option B: Add Comments to the View Context

If you prefer to keep comment_list in your template, override get_context_data in EditTicketView to pass the comments:

class EditTicketView(UpdateView, LoginRequiredMixin):
    model = Ticket
    post_form_class = EditTicketForms
    comment_form_class = CommentForm
    template_name = 'editTicket.html'
    fields = ['ticketName', 'ticketDescription', 'condition', 'priority', 'role']
    success_url = reverse_lazy('dashboard')

    # Add this method
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Fetch comments for the current ticket, sorted by newest first
        context['comment_list'] = self.object.comment.all().order_by('-timestamp')
        return context

3. Clean Up Unnecessary Template Code

Your comment section is wrapped in a <form> tag, but you're just using a link to navigate to the add comment page. This form is unnecessary—remove it to avoid potential issues:

<hr>
<a class="btn btn-default" href="{% url 'add-comment' ticket.id %}">Add comment</a>
<!-- Comment loop goes here -->

After making these changes, create a new comment, and it should now show up on the edit ticket page. The comments will be correctly linked to the ticket, and the template will have access to the comment data to display.

内容的提问来源于stack exchange,提问作者Marc Leese

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:54:12