Django - HTTP 上传多个图像

发布于 2024-10-30 08:59:07 字数 710 浏览 1 评论 0原文

我查看了这个问题:

Uploading multiple files with Django

但它似乎没有帮助因为我有相关问题:

  1. 我不想使用 SWF Upload 和 Uploadify 处理 Flash 会话,因为我需要执行只有经过身份验证的用户才能执行的上传操作。
  2. newforms 适用于旧版本的 django,我正在使用 1.3

使用 Django,我怎样才能拥有这个 HTML 表单结构:

<form enctype="multipart/form-data" action="." method="post">
 <label for="id_image_1">Image 1</label>
 <input type="file" name="image[]" id="id_image_1" />

 <label for="id_image_2">Image 2</label>
 <input type="file" name="image[]" id="id_image_2" />
</form>

并使用视图处理它?

I looked at this question:

Uploading multiple files with Django

but it did not seem to help as I have issues regarding it:

  1. I don't want to deal with flash sessions using SWF Upload and Uploadify because I need to do uploads that only authenticated users can do.
  2. newforms are for older versions of django, I am using 1.3

Using Django, how can I have this HTML form structure:

<form enctype="multipart/form-data" action="." method="post">
 <label for="id_image_1">Image 1</label>
 <input type="file" name="image[]" id="id_image_1" />

 <label for="id_image_2">Image 2</label>
 <input type="file" name="image[]" id="id_image_2" />
</form>

and handle it using a view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷你仙 2024-11-06 08:59:07

如果您有固定数量的文件字段,则可以简单地定义具有足够文件字段的表单,或在表单的构造函数中以编程方式添加文件字段。请参阅有关文件上传的 Django 文档。

如果您想要某种动态功能(例如 gmail 的“添加另一个文件”),那么您可以使用具有单个文件字段的表单来定义表单集。最初显示一个表单,当您想要添加另一个表单时,使用一点 JavaScript 来生成新表单并更新表单集的管理表单。有一个 数字 片段 浮动可以帮助您做到这一点,尽管它们可能需要一些调整。请参阅有关 文件上传表单集

另一种选择可能是使用 自定义小部件和字段,尽管我还没有审查或尝试过这个。

您可能不知道,name="image[]" 方案是 PHP 特定的,在其他语言中没有特殊含义,除非您重新实现它。

If you have a fixed number of filefields, you could simply define a form with enough filefields, or add filefields programatically in a form's constructor. See the Django docs on File Uploads.

If you want some sort of dynamic functionality (a la gmail's "add another file"), then you could define a formset using a form with a single filefield. Display a single form initially and when you want to add another, use a little javascript to produce the new form and update the formset's management form. There are a number of snippets floating around to help you do this, though they may need some tweaking. See the Django docs on File Uploads and Formsets.

Another option may be to use a custom widget and field, though I have not reviewed or tried this.

On the off-chance you aren't aware, the name="image[]" scheme is PHP specific and has no special meaning in other languages, unless you reimplement it.

夜唯美灬不弃 2024-11-06 08:59:07

newforms 是 1.0 之前当前表单的名称。此外,如果您的表单已验证,http://docs.djangoproject .com/en/dev/topics/http/file-uploads/,您将在 request.FILES['image'] 中将文件作为列表(可能是元组,但无论如何都是顺序的),所以只需do:

if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
       for f in request.FILES['image']:
           handle_uploaded_file(f)

你必须自己编写handle_uploaded_file,URL解释了如何

newforms is what the current forms were called before 1.0. Furthermore, if you got your form validated, http://docs.djangoproject.com/en/dev/topics/http/file-uploads/, you'll have your files as a list (tuple, probably, but sequence anyway) in request.FILES['image'], so just do:

if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
       for f in request.FILES['image']:
           handle_uploaded_file(f)

You'll have to write handle_uploaded_file yourself, the URL explains how

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文