上传到 Appengine Blobstore 时处理表单失败
我在 Google App Engine 上使用 @wkornewald 的 django-nonrel 和 django-filetransfer 。
我可以很好地上传文件,但前提是整个表单有效。如果表单的任何字段验证失败,它就会完全崩溃,而不是返回给用户来修复更改。
错误消息是:
INFO 2011-03-10 20:27:09,496 dev_appserver.py:535] Internal redirection to /admin/rr/member/add/
INFO 2011-03-10 20:27:09,662 dev_appserver_blobstore.py:328] Upload handler returned 200
ERROR 2011-03-10 20:27:09,662 dev_appserver_blobstore.py:341] Invalid upload handler response. Only 301, 302 and 303 statuses are permitted and it may not have a content body.
INFO 2011-03-10 20:27:09,680 dev_appserver.py:3317] "POST /_ah/upload/ag5kbXJvbGxpbnJlbGljc3IcCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGIkBDA HTTP/1.1" 500 -
我有一个如下所示的简单模型:
class Member(PhotoMixin, models.Model):
name = models.CharField(max_length=50)
name2 = models.CharField(max_length=50, blank=True)
member_since = models.DateField(blank=True, null=True)
full_size_image = models.FileField(verbose_name="Photo", upload_to='members/')
is_active = models.BooleanField(default=True)
有没有办法可以更优雅地处理这个问题?我觉得这可能是因为 django-filetransfers 希望您将表单发布到 blob 上传 url,而该 url 不知道如何处理失败。当且仅当其余部分有效时,是否应该有一个中间步骤来处理大部分表单并发布到上传网址?
I'm using @wkornewald 's django-nonrel and django-filetransfer on Google App Engine.
I'm able to upload files just fine, but only when the entire form is valid. If the form fails validation for any field, it completely blows up instead of returning to the user to fix the changes.
error message is:
INFO 2011-03-10 20:27:09,496 dev_appserver.py:535] Internal redirection to /admin/rr/member/add/
INFO 2011-03-10 20:27:09,662 dev_appserver_blobstore.py:328] Upload handler returned 200
ERROR 2011-03-10 20:27:09,662 dev_appserver_blobstore.py:341] Invalid upload handler response. Only 301, 302 and 303 statuses are permitted and it may not have a content body.
INFO 2011-03-10 20:27:09,680 dev_appserver.py:3317] "POST /_ah/upload/ag5kbXJvbGxpbnJlbGljc3IcCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGIkBDA HTTP/1.1" 500 -
I have a simple model that looks like this:
class Member(PhotoMixin, models.Model):
name = models.CharField(max_length=50)
name2 = models.CharField(max_length=50, blank=True)
member_since = models.DateField(blank=True, null=True)
full_size_image = models.FileField(verbose_name="Photo", upload_to='members/')
is_active = models.BooleanField(default=True)
Is there a way I can more gracefully handle this? I feel its probably because django-filetransfers wants you to post the form to the blob upload url, and that url doesnt know what to do with a failure. Should there be an intermediate step that handles most of the form and posts to the upload url if and only if the rest is valid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
blobstore api 需要上传处理程序视图返回重定向。
正如上面发布的错误日志所述:
为了解决这个问题,我修改了 ModelAdmin 的 add_view 和 change_view 方法,以在表单验证失败时返回重定向,但附加了查询字符串,以便输入值不会丢失。
接下来,在 GET 请求上初始化表单时,我检查查询参数中的“failed_validation”,以指示将表单数据发送到表单/表单集,以便触发验证。
代码位于 bitbucket 上: https://[电子邮件受保护]/aaronmadison/django-filetransfers。
现在您可以上传到 blobstore 并处理错误...是的。
The blobstore api requires the upload handler view to return a redirect.
as the error log posted above states:
To work around this, I modified the add_view and change_view methods of the ModelAdmin to return a redirect on a form validation failure, but tacked on the query string so the input values are not lost.
Next, when initializing the form on the GET request, I check for 'failed_validation' in the query parameters to indicate to send the form data to the forms/formsets so validation will trigger.
The code is on bitbucket: https://[email protected]/aaronmadison/django-filetransfers.
Now you can upload to the blobstore and handle errors... yay.