为什么不显示表单?

时间:2018-06-28 03:48:18

标签: ajax django

我正在使用django和ajax提交表单,但是当表单无效时,它不会显示。

# views.py
class ProductCreateView(LoginRequiredMixin, CreateView):
    model = Product
    template_name = "products/my_products.html"
    form_class = AddNewProductForm

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.user = self.request.user
        obj.save()
        return JsonResponse({"msg": "Your product was created successfully."})

    def form_invalid(self, form):
        return render(self.request, "products/add_new_product.html", {"form": form}, status=400)


saveNewProductButton.on("click", function () {
        var addNewProductForm = $(".add-new-product-form");
         $.ajax({
        type: 'POST',
        url: '/product/new/',
        data: addNewProductForm.serialize(),
        success: function(res){
            alert(res['msg']);
            location.reload();
        },
         error: function (res) {
            var newProductPlaceHolder = $("#new-product-form-placeholder");
            newProductPlaceHolder.html(res);
         }});
    });

使用Chrome在控制台中显示资源,但在页面上未显示。

0 个答案:

没有答案