虽然我在表单中选择了图像,但图像更改不会保存。其他输入正在保存。请帮帮我。
html格式:
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{settingform}}
<button type="submit">Save</button>
</form>
型号.py:
from django.db import models
class SiteSetting(models.Model):
logo = models.ImageField(blank=True, verbose_name="Logo")
@property
def logo_url(self):
if self.logo and hasattr(self.logo, 'url'):
return self.logo.url
视图.py:
def SiteSettingsView(request):
settings = SiteSetting.objects.all()
settingform = SiteSettingForm(instance=settings[0])
if request.method == 'POST':
settingform = SiteSettingForm(request.POST, instance=settings[0])
if settingform.is_valid():
settingform.save()
return redirect('home')
context = {
'settings': settings[0],
'settingform': settingform
}
return render(request, 'x.html', context)
表单.py:
from site_setting.models import SiteSetting
class SiteSettingForm(forms.ModelForm):
class Meta:
model = SiteSetting
fields = '__all__'
1条答案
按热度按时间tuwxkamq1#
你还必须提供
request.FILES
到SiteSettingForm
:见基本文件上传(django文档)