且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django Ajax表单未保存

更新时间:2023-02-12 10:20:56

我个人将它们分成两个视图,因为它们做的是不同的事情.

I would personally split these up in two views, because they do different stuff.

但是,如果要保持这种方式,可以执行以下操作:

But, if you want to keep it that way, you can do the following:

首先,因为您正在发出AJAX请求,所以您应该返回JsonResponse对象.

First of all, because you are making an AJAX Request, you should return a JsonResponse object.

在您看来,您可以呈现checkout.html并将其作为上下文变量传递给json响应:

In your view you can render the checkout.html and pass it as a context variable to your json response:

def add_user_address(request):
   ...

   data = dict()
   context = {
       'address_form': form,
       ...
   }

   data['html_form'] = render_to_string("checkout.html",
                                        context,
                                        request=request)
   return JsonResponse(data)

在您的$.ajax成功功能中,您可以执行以下操作

And in your $.ajax success function you can do the following

success: function(data) {
  // console.log(data);
  $("div-to-replace-html").html(data.html_form);

}