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

Django POST请求成功但数据库无数据写入的问题排查

表单提交302重定向成功但数据未写入PostgreSQL数据库

我提交表单时得到了成功的302重定向POST请求,但数据并未写入PostgreSQL数据库。我正在实现一个简单的工单评论功能:提交评论后重定向到工单列表页('lists')并显示新评论,流程看似简单,但始终无法将数据存入数据库。

我使用的是PostgreSQL后端。

models.py

class TicketComment(models.Model):
    ticket = models.ForeignKey(TicketList, on_delete=models.CASCADE)
    technician = models.ForeignKey(TechnicianUser, on_delete=models.CASCADE)
    body = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s -%s' % (self.ticket.name, self.ticket)

forms.py

class TicketCommentAddForm(forms.ModelForm):
    class Meta:
        model = TicketComment
        fields = '__all__'

urls.py

# Support Tickets
    path("support-tickets/list", view=apps_tickets_list_view, name="tickets.list"),
    path("support-tickets/edit/<str:pk>", view=apps_tickets_edit_view, name="tickets.edit"),
    path("support-tickets/delete/<str:pk>", view=apps_tickets_delete_list_view, name="tickets.delete_list"),
    path("support-tickets/details/<str:pk>", view=apps_tickets_details_view, name="tickets.details"),
    path("support-tickets/details/<str:pk>/comment", view=apps_tickets_comments_view, name="tickets.comments"),

views.py

def apps_tickets_comments_view(request,pk):
    tickets = TicketList.objects.get(pk=pk)
    comments = TicketComment.objects.all()
    context = {"tickets":tickets,"comments":comments}
    if request.method == "POST":
        form = TicketCommentAddForm(request.POST or None,request.FILES or None,instance=tickets)
        if form.is_valid():
            form.save()
            print(form.cleaned_data['technician'])
            messages.success(request,"Comment inserted Successfully!")
            return redirect("apps:tickets.list")
            
        else:
            print(form.errors)
            messages.error(request,"Something went wrong!")
            return redirect("apps:tickets.list")
            
    return render(request,'apps/support-tickets/apps-tickets-details.html',context)

ticket-details.html

<form action="{% url 'apps:tickets.comments' tickets.identifier %}" method="POST" enctype="multipart/form-data" class="mt-3">
                                            {% csrf_token %}
                                            {{ form.as_p }}
                                            <div class="row g-3">
                                                <div class="col-lg-12">
                                                    <label for="bodyTextarea1" class="form-label">Leave a Comment</label>
                                                    <textarea class="form-control bg-light border-light" id="bodyTextarea1" rows="3" placeholder="Enter comment" name="body"></textarea>
                                                    <input name="technician" type="hidden" name="technician" value="{{user.pk}}">
                                                    <input name="ticket" type="hidden" value="{{tickets.pk}}">
                                                </div>
                                                <div class="col-lg-12 text-end">
                                                    <button type="submit" class="btn btn-success" id="add-btn">Post Comment</button>
                                                </div>
                                            </div>
                                        </form>

相关截图

表单提交成功302重定向并打印表单数据

数据库中无对应数据

我已确认表单设置了method=POST、HTML元素带有name属性,甚至能在控制台打印表单数据,但数据就是无法存入数据库。我忽略了什么?


编辑

我尝试用Postman测试,结果还是一样……我配置了CSRF,GET和POST请求都返回200,也能打印表单数据(技术人员名称)……我快崩溃了

Postman提交后的输出与数据库状态


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 10:20:37