Django文件字段截断request.POST数据

时间:2012-12-17 19:27:40

标签: python django django-forms django-templates django-views

我有一个Django模板,显示如下文件字段:

  <div class="fieldWrapper">
      {{ auth_users_ext.user_pic.error }}
      Image Upload: {{ auth_users_ext.user_pic }}
  </div>

我已经使用了必要的enctype =“multipart / form-data”。该字段正确显示从数据库中提取的值,因此我到目前为止假设它正在按预期运行。我遇到的问题是,无论何时将字段放在模板文件中,request.POST数据都会被截断。

所以,如果我将字段放在表格的最后,我会得到它上面的每一个字段。如果我将字段放在表单的最顶部,我什么也得不到。

我可以在Chrome中读取POST有效负载并验证传递给视图的POST数据是否已完成:

------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--

无论如何,当我在视图中指定request.FILES ['user_pic']时,我什么都没得到。

已更新

以下是该视图中的相关代码:

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

1 个答案:

答案 0 :(得分:1)

此问题最终导致 Google Chrome 出现某种问题。它显然有关于该文件字段的某些问题,并且正在截断返回到服务器的实际POST数据,但是内部处理的内容足以让我认为在检查调试信息时一切都很好。其他所有浏览器都运行得很好。