我在 wagtail 中有一個 Pgae 模型,它持有一個 ImageField:from django.conf import settingsfrom wagtail.users.models import upload_avatar_tofrom 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 formsfrom django.forms import ModelFormclass 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 管理頁面,但是當我檢查個人資料頁面時,頭像圖像不存在,我嘗試調試,但在該過程中沒有發現任何錯誤非常歡迎幫助或建議,非常感謝
1 回答

胡說叔叔
TA貢獻1804條經驗 獲得超8個贊
當您在視圖中重新綁定表單時,您需要通過 -request.FILES
請參閱https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/request.POST
添加回答
舉報
0/150
提交
取消