gpt4 book ai didi

基于 Django 类的 View : Posting form data returns 302 Found status code

转载 作者:行者123 更新时间:2023-12-02 04:23:30 24 4
gpt4 key购买 nike

我正在使用 django 的基于通用类的 View CreateView 将图像上传到书中。代码如下:

# models.py
class Upload(models.Model):
image = models.ImageField(upload_to=get_upload_path, help_text='Image to process')
uploader = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='uploader')
language = models.ForeignKey(Language, models.CASCADE)
book = models.ForeignKey(Book, models.CASCADE)

def __str__(self):
return str(os.path.split(self.image.name)[-1].split('_', 1)[-1])

@models.permalink
def get_absolute_url(self):
return ('book:upload_new', (self.book.id,)) # upload_new is linked to the view below

def save(self, *args, **kwargs):
super(Upload, self).save(*args, **kwargs)

def delete(self, *args, **kwargs):
self.image.delete(False)
super(Upload, self).delete(*args, **kwargs)


# views.py
@method_decorator(login_required, name='dispatch')
class PictureCreateView(CreateView):
model = Upload
fields = ("image",)
book_id = None

def dispatch(self, *args, **kwargs):
# book_id is passed as a URL parameter
self.book_id = self.kwargs['book_id']
return super().dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super(PictureCreateView, self).get_context_data(**kwargs)
context['book_id'] = self.book_id
return context

def form_valid(self, form, **kwargs):
book = Book.objects.get(id=self.book_id)
form.instance.book = book
form.instance.language = book.language
form.instance.uploader = self.request.user
self.object = form.save()

# Running the command below prints
# <TemplateResponse status_code=200, "text/html; charset=utf-8">
# implying the upload was successful
#
# print(self.render_to_response(context=self.get_context_data()))

return super(PictureCreateView, self).form_valid(form)

def form_invalid(self, form):
print(form.errors)
data = json.dumps(form.errors)
return HttpResponse(content=data, status=400, content_type='application/json')

当我尝试上传图像时,我收到此请求的 302 Found 错误(如我的浏览器的开发工具中所示),这意味着页面已暂时移动。除了 header 之外,我无法看到此请求的预览或响应。不过上传成功了(我检查了管理页面)。

有人有 this issue之前,答案指出了 URL conf 中的错误。但是,问题并未完全解决,OP 仍然收到 302 状态代码。

这可能是什么原因?

最佳答案

默认情况下,form_valid 方法使用 302 代码重定向到 success_url(可以通过重写 get_success_url 动态生成)。所以这是正常行为,我认为这是一个很好的做法。所以我不会改变它。

但是如果您确实愿意,您可以返回任何其他响应代码,例如:与 get 内容类似的 200 代码

# views.py
@method_decorator(login_required, name='dispatch')
class PictureCreateView(CreateView):
...

def form_valid(self, form, **kwargs):
book = Book.objects.get(id=self.book_id)
form.instance.book = book
form.instance.language = book.language
form.instance.uploader = self.request.user
self.object = form.save()

context = self.get_context_data()
# You may have check and modify your context for a correct page
# May be: add new things
context["success"] = True

return self.render_to_response(context))

关于基于 Django 类的 View : Posting form data returns 302 Found status code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49065919/

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