来自 ModelForm 的图像未保存
Image from ModelForm not being saved
我在 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",
]
我的view.py:
def profile_details(request: HttpRequest, pk: int = None) -> HttpResponse:
user = cast("User", request.user)
profile = ProfilePage.objects.get(pk=pk)
form = ProfilePageDetailsForm(instance=profile)
if request.method == "POST":
form = ProfilePageDetailsForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect("wagtailadmin_home")
context = {
"user": user,
"form": form,
}
return render(request, "account/profile_details.html", context)
我的模板profile_details.html
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="button" value="{% trans 'submit' %}">
</form>
点击提交并填写包括头像字段在内的所有字段后,我被成功重定向到 wagtail 管理页面
但是当我查看个人资料页面时,头像图像不存在
我尝试调试,但在此过程中没有发现任何错误
非常欢迎任何帮助或建议
非常感谢
当您 re-bind 表单出现在您的视图中时,您需要传递 request.FILES
和 request.POST
- 请参阅 https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/
我在 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",
]
我的view.py:
def profile_details(request: HttpRequest, pk: int = None) -> HttpResponse:
user = cast("User", request.user)
profile = ProfilePage.objects.get(pk=pk)
form = ProfilePageDetailsForm(instance=profile)
if request.method == "POST":
form = ProfilePageDetailsForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect("wagtailadmin_home")
context = {
"user": user,
"form": form,
}
return render(request, "account/profile_details.html", context)
我的模板profile_details.html
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="button" value="{% trans 'submit' %}">
</form>
点击提交并填写包括头像字段在内的所有字段后,我被成功重定向到 wagtail 管理页面 但是当我查看个人资料页面时,头像图像不存在 我尝试调试,但在此过程中没有发现任何错误 非常欢迎任何帮助或建议 非常感谢
当您 re-bind 表单出现在您的视图中时,您需要传递 request.FILES
和 request.POST
- 请参阅 https://docs.djangoproject.com/en/3.1/topics/http/file-uploads/