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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -