I've based my process a minimal file upload - I think based on the answer to the link above. I use:
models.py:
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
views.py:
def list(request):
# Handle file uploadf
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('myproject.myapp.views.list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
forms.py:
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
This saves the file in 'media/documents/2013/08/10/datafile.csv'. You can then access this using the normal open() and read() functions, pointing them to the correct directory and file. As far as I can see, the data remains in the file you upload, but the location and name are stored in the database - in this case, "documents/2013/10/08/datafile.csv".
Hope this helps