David 天宇 Wong
David 天宇 Wong

Reputation: 4167

Django : uploading images

I've been spending many hours trying to find a way to upload images in Django.

If you want to upload it to models it's very easy, but as soon as you don't want models it's impossible, which is weird since it's integrated in the models. Any other framework would have a simple integrated solution. I've looked for plugins, same thing, impossible to find one.

Here is the admin view I added:

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

@staff_member_required
def uploadImage(request):
    if request.method == 'POST':
        form = ImageUploadForm(request.POST, request.FILES)
        if form.is_valid() and form.is_multipart():
            return HttpResponse('so now what?')

    context = { 'form': form }
    return render_to_response('blogs/admin/upload.html', context, RequestContext(request))

So my question: Is there an easy way to upload images with Django without using models?

Upvotes: 0

Views: 408

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

I have absolutely no idea why you keep going on about image uploads being tied to models. It isn't, in any way at all: there is no code in the Model class that deals with image uploads. Even the File and Image fields don't have any code dealing with uploads: all they do is call out to the Storage class, which is exactly what you should do.

clime has already given you the link to the file upload docs. As you should be able to see, they explain fully how to upload a file from a form, with absolutely no mention of models at all.

Upvotes: 1

Related Questions