当尝试在Django应用程序中上传图像时,出现错误“这是必填字段”。即使我实际上是在上传图片。它可以在Admin中正常运行,但不能在App本身中运行。这分别是models.py,forms.py和View。
models.py
class Blog(models.Model):
BLOG_CATEGORIES = (
( 'T', 'Technology'),
( 'B', 'Business' ),
( 'C', 'Culture' ),
( 'N', 'None' )
blogger = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.Cascade)
date_added = models.DateField(auto_now_add=True)
blog_post = models.TextField(max_length=2000)
blog_category = models.CharField(max_length=20, choices=BLOG_CATEGORIES
blog_name = models.CharField(max_length=20, primary_key=True)
blog_picture_context = models.ImageField(upload_to='/static/homepage/', max_length=250)
def __unicode__(self):
return '{} created this entry on {}'.format(self.blogger, self.date_added)
def __str__(self):
return '{} created this entry on {}'.format(self.blogger, self.date_added)
forms.py
class BLogForm(models.ModelForm):
class Meta:
model = Blog
fields = ['blogger', 'date_added', 'blog_post', 'blog_category', 'blog_name', 'blog_picture_context']
views.py
class BlogView(LoginRequiredMixin, FormView):
template_name = 'blog_form.html'
form_class = BlogForm
success_url = '/'
login_url = 'users/login/'
再次,仅出于上下文考虑,我正在尝试在运行开发服务器的应用程序中执行此操作。它给我在ImageField上方的错误。
答案 0 :(得分:0)
也许可以在表单模板中尝试以下操作:
multipart / form-data-必需,以便服务器可以将其感知为文件并上传。