Python Django图像字段未在前面更新
我正在尝试让我的应用程序验证我的应用程序模板中的图像上载字段,并保存上载的任何新图像。在管理方面一切都很好,但是当我在前面更改图像时,它不会被保存 这是我的模型Python Django图像字段未在前面更新,python,django,python-3.x,django-forms,django-templates,Python,Django,Python 3.x,Django Forms,Django Templates,我正在尝试让我的应用程序验证我的应用程序模板中的图像上载字段,并保存上载的任何新图像。在管理方面一切都很好,但是当我在前面更改图像时,它不会被保存 这是我的模型 def imageupload(instance, filename): return os.path.join('static/petition-photos/', filename) # Create your models here. class Petition(models.Model): title =
def imageupload(instance, filename):
return os.path.join('static/petition-photos/', filename)
# Create your models here.
class Petition(models.Model):
title = models.CharField(max_length= 90, default="Enter petition title here")
created_on = models.DateTimeField(auto_now_add=True)
image = models.ImageField(null=False, upload_to=imageupload)
video = models.CharField(max_length=600, default="Enter an external video link")
petition = models.TextField(null=False, default="Type your petition here")
created_by = models.ForeignKey(User)
这是我的观点课:
class NewPetitionView(generic.edit.CreateView):
model = Petition
template_name = 'petition/petition_form.html'
fields= ['title','petition', 'image', 'video']
success_url = '/dashboard/'
def form_valid(self, form):
form.instance.created_by = self.request.user
return super(NewPetitionView, self).form_valid(form)
这是我的表单模板:
{% include 'layout/header.html' %}
{% block content %}
<form action="" method="post">
{{form.as_p}}
<button type="submit">Submit</button>
{% csrf_token %}
</form>
{% endblock %}
当我上传一张图片到一篇新文章或试图编辑其中一篇文章的图片字段时,我会收到一个“此字段是必需的”通知
我做错了什么?您需要在add enctype=multipart/form数据中添加文件上传功能