I have a Django template which is displaying a file field like so:
<div class="fieldWrapper">
{{ auth_users_ext.user_pic.error }}
Image Upload: {{ auth_users_ext.user_pic }}
</div>
I’ve used the necessary enctype=”multipart/form-data”. The field correctly displays a value being pulled from the database, so I have so far assumed that it is functioning as it’s supposed to. The problem I’m having is that wherever I place the field within the template file, the request.POST data is truncated at that point.
So, if I place the field last on the form, I get every field above it. If I place the field at the very top of the form, I get nothing at all.
I can read the POST payload in Chrome and verify that the POST data being passed to the view is complete:
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="csrfmiddlewaretoken"
********
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="csrfmiddlewaretoken"
********
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="staff_id"
98.0
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="auth_user_id"
1069
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="user_groups"
1
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="user_groups"
11
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="user_groups"
13
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="current_pic"
/media/no_pic.jpg
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="user_pic"; filename="Picture0029.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="first_name"
Robert
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="last_name"
Vila
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="username"
bobvila
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="email"
BobVila@thisoldhouse.com
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="work_number"
1239111234
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="mobile_number"
1239111234
------WebKitFormBoundaryZTdhKmOKbDRAXLAm
Content-Disposition: form-data; name="mgr_id"
1
------WebKitFormBoundaryZTdhKmOKbDRAXLAm--
Regardless, when I specify request.FILES[‘user_pic’] in the view, I get nothing.
UPDATED
Here is the relevant code from the view:
def STAFF(request, uid=None, template='auth_user.html'):
[ ... ]
user = request.user
is_admin = user.groups.filter(name='*** ADMIN_GROUP_NAME ***')
if uid == None:
instance = AuthUser()
ext_instance = AuthUserExt()
else:
instance = AuthUser.objects.get(auth_user_id=uid)
ext_instance = AuthUserExt.objects.get(auth_user_id=uid)
if request.method == 'POST':
# get image request.FILE object
if request.FILES:
avatar = request.FILES['user_pic']
# build destination path for os file handling
dest_path = settings.MEDIA_ROOT + request.POST['username'] + '/avatar/'
# create user/avatar dir if not exist
if not os.path.exists(dest_path):
os.makedirs(dest_path)
# open file handle at the intended destination and write our request.FILE
if os.path.isfile(dest_path + avatar.name):
os.remove(dest_path + avatar.name)
destination = open(dest_path + avatar.name, 'wb+')
destination.write(avatar.read())
destination.close()
# build uri path for database insert
uri_path = settings.MEDIA_URL + request.POST['username'] + '/avatar/' + avatar.name
else:
[ ... ] # lands here, because Django produces no request.FILES
This issue ended up being the result of some sort of problem with Google Chrome. It was apparently having a problem with something about that file field and was truncating the actual POST data being returned to the server, but was handling things internally sufficient to make me think that all was well when I inspected the debug information. Every other browser worked just fine.