Django ModelForm Imagefield Upload

后端 未结 2 615
旧时难觅i
旧时难觅i 2021-02-02 12:11

I am pretty new to Django and I met a problem in handling image upload using ModelForm. My model is as following:

class Project(models.Model):
    name = models.         


        
相关标签:
2条回答
  • 2021-02-02 12:44

    OK, thanks everyone. I found myself made a stupid mistake. I should add enctype="multipart/form-data" in the <form> Else, it won't upload the file.

    0 讨论(0)
  • 2021-02-02 13:04

    You shouldn't need to set the newproject.photo to request.FILES['photo'] (actually this right here probably breaks your code). Just save the addprojectform with addprojectform.save() instead of newproject = addprojectform.save(commit=False). See more info here: https://docs.djangoproject.com/en/1.5/topics/http/file-uploads/#handling-uploaded-files-with-a-model

    0 讨论(0)
提交回复
热议问题