1

我有一个 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
4

1 回答 1

2

这个问题最终是由Google Chrome的某种问题造成的。显然,该文件字段存在问题,并且正在截断返回到服务器的实际 POST 数据,但是在内部处理的事情足以让我在检查调试信息时认为一切都很好。每个其他浏览器都工作得很好。

于 2013-01-09T21:43:04.750 回答