使用 Django 上传多个文件 [英] Uploading multiple files with Django

查看:21
本文介绍了使用 Django 上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Django 上传多个文件?

How do I upload multiple files with Django?

推荐答案

经过一番苦恼,我终于搞定了uploadify (http://www.uploadify.com/) 与 django 一起工作,但问题不是真正的 django,而是让它与 Apple Mac 一起工作;该平台上的浏览器不会从 Flash 中提供 cookie;您需要手动设置它们:

After a lot of pain I eventually got uploadify (http://www.uploadify.com/) working with django, but the problem wasn't really django's, but getting it to work with Apple Mac's; browsers on that platform don't serve the cookies from within Flash; you need to set them manually:

所以我将这些包含在我的渲染到响应中:

So I include these in my render-to-reponse:

return render_to_response('files_upload.html', {
       'session_cookie_name': settings.SESSION_COOKIE_NAME,
       'session_key': request.session.session_key

并通过模板中的配置从 uploadify 中呈现它们:

And present them from uploadify via the configuration laid down in the template:

$(document).ready(function() {
    $('#fileInput').uploadify({
        'scriptData': {'{{session_cookie_name}}': '{{session_key}}'},

我已经看到在视图上使用装饰器做得更好,但这是我在中间件中放置的肮脏黑客,用于在会话中间件运行以恢复会话之前将 POST 复制到 COOKIE.

I've seen this done better with a decorator over the view, but this was the dirty hack I put in middleware to copy the POST into COOKIE before the session middleware runs to restore the session.

class FakeUploadCookieMiddleware(object):
   """TODO: replace the hardcoded url '/upload' with a 'reverse'."""
   def process_request(self, request):
       if request.path == '/upload/' 
           and request.POST.has_key(settings.SESSION_COOKIE_NAME):
           request.COOKIES[settings.SESSION_COOKIE_NAME] = 
               request.POST[settings.SESSION_COOKIE_NAME]
           logging.debug('Faking the session cookie for an upload: %s', 
               request.POST[settings.SESSION_COOKIE_NAME])

这篇关于使用 Django 上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆