重定向中断图片上传

Redirection disrupts image upload

当用户提交表单时,用户应该被重定向到其他页面。我已经使用 window.location.href 进行重定向,但它不起作用。它重定向但上传图片将无法正常工作。

这是我的代码:

AddRent.js(上传图片代码更注重缩短代码行)

 $.ajax({
        url:"/add/space/",
        data:sendData,
        type:'POST',
        success: function(data, textStatus, xhr ) {
          var pk = xhr.getResponseHeader('pk-user');
          console.log('pk is',pk);
          $.ajax({
           url:"/upload/image/"+pk+"/",
           data:image,
           contentType:false,
           processData:false,
           type:'POST',
           mimeType: "multipart/form-data",
           success: function(data) {
             console.log('success');
           }
          });
         window.location.href="http://commonrentpspace.me/"; // if i use this to redirect, the images does not get upload
        }
       });
      }
}

Views.py:

class AddView(TemplateView):
    template_name = 'rentals/add.html'

class AddSpaceView(View):
    def post(self,request,*args,**kwargs):
        print ('add space view',request)
        if request.POST:
            response = HttpResponse('')
            print('owner name is',request.POST.get('ownerName'))
            print('amenities',request.POST.get('amenities'))
            rental = Rental()
            rental.ownerName = request.POST.get('ownerName')
            rental.email = request.POST.get('email')
            rental.phoneNumber = request.POST.get('phoneNumber')
            rental.listingName = request.POST.get('listingName')
            rental.summary = request.POST.get('summary')
            rental.property = request.POST.get('property')
            rental.room = request.POST.get('room')
            rental.price = request.POST.get('price')
            rental.city = request.POST.get('city')
            rental.place = request.POST.get('place')
            rental.water = request.POST.get('water')
            rental.amenities = request.POST.get('amenities')
            rental.save()
            response['pk-user'] = rental.pk
            return response

        return HttpResponse('Rental Information successfully added')


 class UploadImage(View):
    model = Rental
    template_name = 'rentals/add.html'
    print "Hello"
    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)
    def post(self,request,*args,**kwargs):
        try:
            rental = Rental.objects.get(pk = self.kwargs['pk'])
        except Rental.DoesNotExist:
            error_dict = {'message': 'Rental spae not found'}
        print "Error Rental do not exist"
            return self.render(request,'rentals/add.html',error_dict)
        if request.FILES:
            for file in request.FILES.getlist('image'):
                print('file',file)
                image = Gallery.objects.create(rental = rental, image=file)
                print('image',image)
        print "Uploading Image"
        return HttpResponse("Uploaded successfully")

我还需要提供其他信息吗?可能是什么原因?

$.ajax({
        url:"/add/space/",
        data:sendData,
        type:'POST',
        success: function(data, textStatus, xhr ) {
          var pk = xhr.getResponseHeader('pk-user');
          console.log('pk is',pk);
          $.ajax({
           url:"/upload/image/"+pk+"/",
           data:image,
           contentType:false,
           processData:false,
           type:'POST',
           mimeType: "multipart/form-data",
           success: function(data) {
             console.log('success');
             window.location.href="http://commonrentpspace.me/"; // move it here.
           }
          });
        }
       });
      }
}