Ajax 为什么不显示表单?
我使用django和ajax提交表单,但是当表单无效时,它就不会显示Ajax 为什么不显示表单?,ajax,django,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
# 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);
}});
});
res使用Chrome显示在控制台中,但不显示在页面上。
form\u class
应该是类,而不是实例。尝试此form\u class=AddNewProductForm
@neverwalklanoner我已更正,但它没有更改任何内容。您在form\u中使用的模板无效。检查表单是否使用“products/add_new_product.html”
中的变量。