ModelForm 中的图像未保存

我在 wagtail 中有一个 Pgae 模型,它持有一个 ImageField:


from django.conf import settings

from wagtail.users.models import upload_avatar_to

from django.utils.translation import gettext_lazy as _


class ProfilePage(Page):

    avatar = models.ImageField(

        # Also updated from user.wagtail_userprofile.avatar

        verbose_name=_("profile picture"),

        upload_to=upload_avatar_to,

        blank=True,

    )

    intro = RichTextField(

        blank=True,

        null=True,

        verbose_name=_("Personal introduction"),

        help_text=_("maximum number of characters: 80 characters"),

        max_length=80,

    )

    school = models.CharField(

        verbose_name=_("School name"), max_length=100, blank=True, null=True

    )

    school_details = models.CharField(

        blank=True,

        null=True,

        verbose_name=_("School details"),

        help_text=_("example: Faculty of Medicine"),

        max_length=100,

    )

    location = models.CharField(

        verbose_name=_("Location"),

        max_length=50,

        blank=True,

        null=True,

        help_text=_("example: Osaka"),

    )

    user = models.ForeignKey(

        settings.AUTH_USER_MODEL, on_delete=models.PROTECT, verbose_name=_("User")

    )


我需要创建一个模型表单,因为我正在渲染自定义模板以在注册后设置个人资料:


from django import forms

from django.forms import ModelForm


class ProfilePageDetailsForm(ModelForm):

    intro = forms.CharField(

        required=False,

        widget=forms.Textarea,

        label=_("Personal introduction"),

        help_text=_("maximum number of characters: 80 characters"),

        max_length=80,

    )


    class Meta:

        model = ProfilePage

        fields = [

            "avatar",

            "title",

            "school",

            "location",

            "intro",

        ]

单击提交并填写包括头像字段在内的所有字段后,我成功重定向到 wagtail 管理页面,但是当我检查个人资料页面时,头像图像不存在,我尝试调试,但在该过程中没有发现任何错误非常欢迎帮助或建议,非常感谢


万千封印
浏览 96回答 1
1回答

胡说叔叔

当您在视图中重新绑定表单时,您需要通过 -request.FILES请参阅https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/request.POST
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python