【发布时间】:2012-12-04 22:34:02
【问题描述】:
我有一个 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
【问题讨论】:
-
请出示您的查看代码。
-
您是否检查过 Python Imaging Library (PIL) 是否安装正确?
-
@zubair89:模型中没有实际的文件处理。唯一的服务器端处理是移动上传的文件,但它并没有走那么远,因为 Django 在遇到此字段时无法生成
request.POST\FILES数据。这段代码在五个月前第一次测试时就可以工作,但是从那时起环境的很多事情都发生了变化,我无法知道问题可能是什么。日志没有表明我可以检测到,只是显示 500 错误。
标签: python django django-forms django-templates django-views