Last Updated: February 25, 2016
·
812
· mimoo

Simple Image/File Uploader in Django

I couldn't really find anything on internet, and it's the first time I come across a framework that doesn't include a class for that.

So here you go, no security, it was made for admins only, it checks for same name files and add a number after until there is no other file with the same name.

from django.shortcuts import render, render_to_response
from django.contrib.admin.views.decorators import staff_member_required
from django.template import RequestContext
from django import forms
from django.http import HttpResponse
from django.conf import settings

import os

def save_file(file, path = 'upload/'):
    filename = file._get_name()

    path2 = settings.MEDIA_ROOT + str(path) + str(filename)

    ii = ''
    if(os.path.exists(path2)):
        ii = 1
        while os.path.exists(path2):
            ii = ii + 1
            path2 = path2 + str(ii)
    fd = open(path2, 'wb')
    for chunk in file.chunks():
        fd.write(chunk)
    fd.close()
    return '![alt](settings.MEDIA_URL + '/' + str(path) + str(filename) + str(ii) + ')'

class ImageUploadForm(forms.Form):
    image = forms.ImageField()

@staff_member_required
def uploadImage(request):
    url = ''
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid() and form.is_multipart():
            url = save_file(request.FILES['image'])
        else:
            form = ImageUploadForm()
    else:
        form = ImageUploadForm()
    context = { 'form': form, 'url': url}
    return render_to_response('blogs/admin/upload.html', context, RequestContext(request))

1 Response
Add your response

thank you!

over 1 year ago ·