I recently found a really good minimalist example showing how to upload files using Python and DJango
http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example
It works just great on my server. However, I need to do something just a little different that should work, but doesn’t. See subsection number 4, where he has the portion of the “views.py” file code that reads as follows:
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
Instead, in my use case scenario, it looks more like this:
newdoc = Document()
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
for each in form:
if each.name != "ID":
if type(each.field) is forms.FileField:
setattr(newdoc, each.name, request.FILES[each.name])
else
setattr(newdoc, each.name, form.cleaned_data[each.name])
newdoc.save()
Since “setattr” effectively does the same thing as his original example does, and since the model field and form field names are identically named “docfile”, I’m effectively trying to do this:
setattr(newdoc, 'docfile', request.FILES['docfile'])
You can copy and paste my code right into his example to test. But unfortunately its not working. Can anyone help point me in the right direction as to what I might be doing wrong here?