Right I\'m learning how to do a simple image upload form to upload an image to MEDIA_ROOT. The form renders fine, I get no errors, but the file is not showing up in the MEDIA_RO
You don't need to attach it to a model as Matt S states, request.FILES has all the data for the image if your form is set up correctly.
You need to make sure use enctype="multipart/form-data"
in your element, as the docs state here: https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form
Aside from that, read the docs about file uploads: https://docs.djangoproject.com/en/dev/topics/http/file-uploads/
All you need is well documented there. Hope this helps!
EDIT OP requested more information on File Uploads
From the docs:
Most of the time, you'll simply pass the file data from request into the form as described in Binding uploaded files to a form. This would look something like:
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form})
And they have an example of how you could write the handle_uploaded_file
function:
def handle_uploaded_file(f):
destination = open('some/file/name.txt', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
In order to have your files uploaded and shows in request.FILES, your form MUST contain enctype="multipart/form-data" as shown below:
<form action="{% url scvd.views.merchant_image_upload %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.image }}
<input type="submit" value="Upload" />
</form>
You need to attach it to a model (as an ImageField) as it's not being saved anywhere presently. It may be handled without any issues right now but it gets discarded after the view returns.
Never mind, I didn't realize models were unnecessary.