我一直在尝试更新空间信息以及同一模板上的图像。我可以添加内容但无法更新。我试图传递slug,同时提交更新的空间信息以更新图像但是slug显示为null。我的ajax代码和视图适用于添加部分,但不适用于更新部分。我得到我的console.log('租用工作')打印成功功能,但请求。邮件'是不是打印在EditSpaceView函数里面可能是什么原因?我该怎么做才能让它发挥作用?
views.py
class EditSpaceView(View):
def post(self,request,*args,**kwargs):
print ('edit space view',request)
if request.POST:
print('request.post')
response = HttpResponse('')
print('edited owner name is',request.POST.get('ownerName'))
print('edited amenities',request.POST.get('amenities'))
rental = Rental.objetcs.get(slug=self.kwargs['slug'])
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.slug
#response['pk-user'] = rental.pk
print('response slug', response);
return response
return HttpResponseRedirect('/')
class EditImage(View):
model = Rental
template_name = 'rentals/rent_detail.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self,request,*args,**kwargs):
try:
rental = Rental.objects.get(slug = self.kwargs['slug'])
print('rental slug',rental)
except Rental.DoesNotExist:
error_dict = {'message': 'Rental spae not found'}
return self.render(request,'rentals/rent_detail.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)
return HttpResponseRedirect('/')
urls.py
url(r'^edit/image/(?P<slug>[\w-]+)/$', EditImage.as_view(), name="editImage"),
url(r'^edit/space/(?P<slug>[\w-]+)/$', EditSpaceView.as_view(), name="editSpace"),
ajax代码
$.ajax({
url:'/edit/space/'+this.props.slug+'/',
contentType: "application/json",
data:sendData,
type:'POST',
success: function(data, textStatus, xhr ) {
console.log('rent worked');
var slug = xhr.getResponseHeader('pk-user');
console.log('slug',slug);
$.ajax({
url:'/edit/image/'+slug+'/',
data:image,
contentType:false,
processData:false,
type:'POST',
mimeType: "multipart/form-data",
success: function(data) {
console.log('success');
}
});
}
});
服务器控制台中的Stacktrace
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
Not Found: /edit/image/null/
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
答案 0 :(得分:1)
[05/Apr/2016 02:58:35] "GET /api/v1/rental/tushant-khatiwada/ HTTP/1.1" 200 963
edit space view <WSGIRequest: POST '/edit/space/tushant-khatiwada/'>
[05/Apr/2016 02:58:50] "POST /edit/space/tushant-khatiwada/ HTTP/1.1" 302 0
[05/Apr/2016 02:58:51] "GET / HTTP/1.1" 200 2363
这些请求表明使用EditSpaceView
进行编辑符合预期。 300
响应是预期的,并且由于该视图末尾的HttpResponseRedirect("/")
。
请求
[05/Apr/2016 02:58:51] "POST /edit/image/null/ HTTP/1.1" 404 6882
建议您用于构建ajax请求的URL的slug
无效或为null。具体检查这一行,
var slug = xhr.getResponseHeader('pk-user');