python - Django - Form FileField error "This field is required" -
i want add post form django project , i've got problem filefiled. here code:
forms.py
class postform(forms.modelform): class meta: model = post fields = [ 'author', 'image', 'title', 'body' ]
models.py
class post(models.model): author = models.foreignkey('auth.user') image = models.filefield(default="", blank=false, null=false) title = models.charfield(max_length=200) body = models.textfield() date = models.datetimefield(default=timezone.now, null=true) def approved_comments(self): return self.comments.filter(approved_comment=true) def __str__(self): return self.title
if helps. set enctype="multipart/form-data
in <form>
thanks help.
class post(models.model): author = models.foreignkey('auth.user') image = models.filefield(upload_to='path') title = models.charfield(max_length=200) body = models.textfield() date = models.datetimefield(default=timezone.now, null=true) def approved_comments(self): return self.comments.filter(approved_comment=true) def __str__(self): return self.title
you need mention upload_path in filefield
add enctype="multipart/form-data
form
and in view files
postform(request.post, request.files)
if need make field optional
class postform(forms.modelform): image = forms.filefield(required=false) class meta: model = post fields = [ 'author', 'image', 'title', 'body' ]
Comments
Post a Comment