cclerv
cclerv

Reputation: 2969

Uploading images in Django not working

I am trying to use Django to upload images to a directory on disk. For some reason I'm can't get this to work properly. I get redirected to the submit-success.html page with no errors, however the file doesn't get uploaded. I've provided some code below. Any help would be greatly appreciated. Thanks in advance.

NB: I'm working on the development server, I'm using django 1.3, I'm using sqlite3

This is my form in upload.html

 <form enctype="multipart/form-data" action="{% url upload_success %}" method="post">
 {% csrf_token %}
 <table>{{ form }} </table>
 <input type="submit" value="Submit image">
 </form>

Here is a link to some code I have written: https://gist.github.com/1468190

Upvotes: 0

Views: 316

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239470

Your handle_uploaded_image method seems faulty. For example, with these lines:

photo_dir = '%s/uploaded_photos/Not_Published/%Y/%m/%d' % settings.MEDIA_ROOT
photo_destination = open(photo_dir, 'wb+')

You're attempting to open a file in "YYYY/MM" with the name of "DD". If you already have "DD" as a directory, this will not work, and if you don't your image is going to be saved as "DD" not as "image.jpg" or whatever. So your image may very well be getting saved, but you're not recognizing it.

Upvotes: 1

Related Questions