为什么在成功的Django POST中数据库没有收到数据?

zvokhttg  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(186)

我在提交表单时收到了一个成功的302重定向POST,但数据没有填充到数据库中。我正在创建一个简单的评论。获取评论,提交它,重定向到前一页(“列表”),显示新的评论-相当简单-但我不明白为什么我在数据库中没有得到任何数据
我正在使用postgres后端
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>

我已经确保method=POST,我在html中有一个name属性,甚至可以在控制台中打印表单数据,但无法在数据库中获取它。我忽略了什么?

编辑

我走的路线尝试 Postman 和仍然相同的事情...我设置了CSRF,在GET和POST请求上有一个200。我可以打印回表单数据(技术名称)....我失去理智了

gcuhipw9

gcuhipw91#

你将tickets.identifier传递给你的url,但我看不到这是一个值,这意味着你有一个无效的url。

<form action="{% url 'apps:tickets.comments' tickets.identifier %}"

另外,对于这类问题,我建议将视图与表单分开测试(编写一个测试用例,或者使用postman或其他方法),这样更容易知道哪个部分出错了。

相关问题