I am trying to use django to upload images but its not working as
expected. Here is what I'm working with

 #handle_uploaded_image.py
    def handle_uploaded_image(photo, caption, source, more_info):
        photo_dir = '%s/uploaded_photos/Not_Published/%Y/%m/%d' %
settings.MEDIA_ROOT
        photo_destination = open(photo_dir, 'wb+')
        for chunk in photo.chunks():
            destination.write(chunk)
        photo_destination.close()

        info_file = '%s/uploaded_photos/Not_Published/%Y/%m/%d/
PHOTO_INFO.txt' % settings.MEDIA_ROOT
        write_info = "name: %s\n caption: %s\n source: %s\n more_info:
%s\n\n" % photo.name, caption, source, more_info
        photo_info_destination = open(info_file, 'a')
        photo_info_destination.write(write_info)
        photo_info_destination.close()

    #views.py
    def submit_image(request):
        if request.method == 'POST':
            form = UploadImageForm(request.POST, request.FILES)
            if form.is_valid():
                uploadedImage = form.cleaned_data['image']
                caption = form.cleaned_data['caption']
                source = form.cleaned_data['source']
                more_info = form.cleaned_data['more_info']

                handle_uploaded_image(uploadedImage, caption, source,
more_info)
                return redirect('photos.views.upload_success')
        else:
            form = UploadImageForm()
        return render(request,'photos/upload.html', {'form': form})

    def upload_success(request):
        return render(request, 'photos/submit-success.html')

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to