2016-04-25 4 views
0

ユーザーがフォームを送信すると、ユーザーは別のページにリダイレクトされます。リダイレクトに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") 

他の情報を提供する必要がありますか?何が原因だろうか?

+0

別のものの成功関数内でajaxを呼び出しています。 これは良い方法ではありません。 '' 'window.location.href =" http://commonrentpspace.me/ ";' ''を内側のajaxに移動します。それがあなたの問題を解決します。 – iraycd

+0

あなたは内部の成功関数(内側のajax)を言うことを意味しますか? – pri

+0

はい、私は以下を意味します。 '' 'console.log( 'success');' '' – iraycd

答えて

1
$.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. 
      } 
      }); 
     } 
     }); 
     } 
} 
関連する問題