上传到Appengine Blobstore时处理表单失败 [英] Handle Form Failure when uploading to Appengine Blobstore

查看:94
本文介绍了上传到Appengine Blobstore时处理表单失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google App Engine上使用了@wkornewald的django-nonrel和django-filetransfer。

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.

错误信息是:


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)

有没有办法更好地处理这个?我觉得它可能是因为django-filetransfers希望你将表单发布到blob上传URL,而且该url不知道如何处理失败。是否应该有一个中间步骤来处理大部分的表单和发布到上传网址,当且仅当其余的有效?

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?

推荐答案

p> blobstore api需要上传处理程序视图来返回重定向。

The blobstore api requires the upload handler view to return a redirect.

作为上面发布的错误日志状态:

as the error log posted above states:


Only 301, 302 and 303 statuses are permitted and it may not have a content body.

为了解决这个问题,我修改了ModelAdmin的add_view和change_view方法以返回一个重定向表单验证失败,但是在查询字符串上加上输入值不会丢失。

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.

接下来,在GET请求初始化表单时,我检查'failed_validation'在查询参数中指示将表单数据发送到表单/表单,以便验证将触发。

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.

代码位于bitbucket: https://aaronmadison@bitbucket.org/aaronmadison/django-filetransfers

The code is on bitbucket: https://aaronmadison@bitbucket.org/aaronmadison/django-filetransfers.

现在您可以上传到blobstore并处理错误... yay。

Now you can upload to the blobstore and handle errors... yay.

这篇关于上传到Appengine Blobstore时处理表单失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆