gpt4 book ai didi

python - 我在更新空间信息和图像时收到状态代码 300

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:50 25 4
gpt4 key购买 nike

我一直在尝试更新空间信息以及同一模板上的图像。我可以添加内容,但无法更新。我尝试在提交更新空间信息以更新图像时传递 slug,但是slug 显示为空。我的 ajax 代码和 View 适用于添加部分,但不适用于更新部分。我在 success 函数中打印了 console.log('rent Work') 但在 EditSpaceView 函数中未打印 'request.post' 可能是什么原因?我应该怎么做才能让它发挥作用?

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('/')

url.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');
}
});
}
});

服务器控制台中的堆栈跟踪

[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

最佳答案

[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 响应是预期的,并且是由于该 View 末尾的 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');

关于python - 我在更新空间信息和图像时收到状态代码 300,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com