javascript - Redirection disrupts image upload -
when user submits form, user should redirected other page. have used window.location.href redirection not working. redirects uploading image won't work then.
here code:
addrent.js(upload image code focused more shortening code line)
$.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 use redirect, images not 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 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 not exist" return self.render(request,'rentals/add.html',error_dict) if request.files: 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")
do need provide other information? might cause?
$.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 here. } }); } }); } }
Comments
Post a Comment