Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango File Field Truncates request.POST Data
    primarykey
    data
    text
    <p>I have a Django template which is displaying a file field like so:</p> <pre><code> &lt;div class="fieldWrapper"&gt; {{ auth_users_ext.user_pic.error }} Image Upload: {{ auth_users_ext.user_pic }} &lt;/div&gt; </code></pre> <p>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.</p> <p>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.</p> <p>I can read the POST payload in Chrome and verify that the POST data being passed to the view is complete:</p> <pre><code>------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-- </code></pre> <p>Regardless, when I specify request.FILES['user_pic'] in the view, I get nothing.</p> <p><strong>UPDATED</strong></p> <p>Here is the relevant code from the view:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload