在上传至Appengine Blobstore时处理表单失败

时间:2021-09-14 15:35:43

I'm using @wkornewald 's django-nonrel and django-filetransfer on Google App Engine.

我正在谷歌应用引擎上使用@wkornewald的django-nonrel和django-filetransfer。

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?

有什么方法可以让我更优雅地处理这个问题吗?我觉得这可能是因为django-filetransfer希望您将表单发布到blob上传url,而这个url不知道如何处理失败。如果且仅当其余部分有效时,是否应该有一个中间步骤来处理大多数表单和post到上载url ?

1 个解决方案

#1


2  

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

blobstore api要求上传处理程序视图返回重定向。

as the error log posted above states:

如上述错误日志所述:


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

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.

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

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.

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

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

代码在bitbucket上:https://aaronmadison@bitbucket.org/aaronmadison/django- file转账。

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

现在您可以上传到blobstore并处理错误…耶。

#1


2  

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

blobstore api要求上传处理程序视图返回重定向。

as the error log posted above states:

如上述错误日志所述:


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

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.

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

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.

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

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

代码在bitbucket上:https://aaronmadison@bitbucket.org/aaronmadison/django- file转账。

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

现在您可以上传到blobstore并处理错误…耶。