Django ModelForm Imagefield Upload
Asked Answered
U

2

22

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.CharField(max_length=100)
    description = models.CharField(max_length=2000)
    startDate = models.DateField(auto_now_add=True)
    photo = models.ImageField(upload_to="projectimg/", null=True, blank=True)

And the modelform is as following:

class AddProjectForm(ModelForm):
    class Meta:
        model = Project
        widgets = {
            'description': Textarea(attrs={'cols': 80, 'rows': 50}),
        }
        fields = ['name', 'description', 'photo']

And the View function is:

def addProject(request, template_name):
if request.method == 'POST':
    addprojectform = AddProjectForm(request.POST,request.FILES)
    print addprojectform
    if addprojectform.is_valid():
        newproject = addprojectform.save(commit=False)
        print newproject
        print request.FILES
        newproject.photo = request.FILES['photo']
        newproject.save()
        print newproject.photo
else:
    addprojectform = AddProjectForm()

newProposalNum = projectProposal.objects.filter(solved=False).count()
return render(request, template_name, {'addprojectform':addprojectform,
                                       'newProposalNum':newProposalNum})

the template is:

<form class="bs-example form-horizontal" method="post" action="">{% csrf_token %}
                      <h2>Project Name</h2><br>
                      {{ addprojectform.name }}<br>
                      <h2>Project Description</h2>
                      {{ addprojectform.description }}<br>
                      <h2>Image Upload</h2><br>
                      {{ addprojectform.photo }}<br>
                      <input type="submit" class="btn btn-success" value="Add Project">
                  </form>

Can anyone help me or could you give an example of image uploading? Thank you!

Ulloa answered 28/10, 2013 at 7:14 Comment(5)
Can you mention what is your problem...?Effloresce
my problem is that the image is not uploaded to the directory.Ulloa
Show us your template.Schofield
a new project is commited to the database but without the uploaded file ? do you have a link to that file but no access to it ?Thorin
yes, the project is committed to the database but the image is not uploaded. I specified where to store the image in the model.Ulloa
U
57

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.

Ulloa answered 28/10, 2013 at 15:49 Comment(1)
Thanks for the post, for my case, even all set is the same as yours with enctype, but at the beginning, it still dose not upload the image/file. I re-start the web-page, and re-run for several times, and finally, it works. Your post helped me to confirm my setting is correct. ThanksAlbany
S
2

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

Suntan answered 28/10, 2013 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.