如何在表单向导中的 clean 方法之前保存提交的表单图像字段(Django 1.2)

发布于 2024-12-16 00:39:55 字数 494 浏览 6 评论 0原文

表单向导中提交表单时,我收到图像字段的“此字段为必填”错误。

class SignupForm(forms.ModelForm):
    username = forms.CharField(
        label = _("Username*"),
        max_length = 30,
        widget = forms.TextInput()
    )
    image = forms.ImageField(label = _("Profile picture*"), required=True)
class Meta:
    model = Profile
    fields = ('name','gender','birth_date',)

这是我在表单向导中的最后一个表单。提交此表单后,我在 clean 方法中获得了用户名和其他字段,但没有获得图像。有没有办法在验证此表单之前保存图像以避免“此字段为必填”错误?

I get "This field is required" error for image field on submitting a form in Form Wizard.

class SignupForm(forms.ModelForm):
    username = forms.CharField(
        label = _("Username*"),
        max_length = 30,
        widget = forms.TextInput()
    )
    image = forms.ImageField(label = _("Profile picture*"), required=True)
class Meta:
    model = Profile
    fields = ('name','gender','birth_date',)

This is my last form in form wizard. On submitting this form I got username and other fields in clean method but not the image. Is there a way to save image before validation of this form to avoid "This field is required" error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2024-12-23 00:39:55

要通过表单向导上传图像,您应该更改文件“django/contrib/formtools/wizard.py”。此票证中提到了这些更改 code.djangoproject.com/ticket/7439。如果您不想更改 djnago 代码,则只需将“formtools”文件夹复制到您的应用程序中,然后在其中进行更改,而不是使用:

from django.contrib.formtools.wizard import FormWizard

使用:

from formtools.wizard import FormWizard

同时设置模板表单的 enctype="multipart/form-data" 。

To upload your images through Form Wizard you should change the file "django/contrib/formtools/wizard.py". These changes are mentioned in this Ticket code.djangoproject.com/ticket/7439. If you dont want to change djnago code then simply copy "formtools" folder into your apps then make changes there and instead of using:

from django.contrib.formtools.wizard import FormWizard

use:

from formtools.wizard import FormWizard

Also set enctype="multipart/form-data" of your template form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文