django - 限制django表单文件上传的文件扩展名

标签 django forms file-upload

我在 django 中有 forms.Form。在这个表单中有一个forms.FileField。该字段在 html 中呈现为

<input id="id_upload-loaded_file" name="upload-loaded_file" type="file">

我需要添加仅带有“.zip”扩展名的接受文件,以便表单将呈现为

<input id="id_upload-loaded_file" name="upload-loaded_file" type="file" accept=".zip">

如何在 django 中添加接受的文件扩展名属性?

最佳答案

我就是这样做的:

zip_file = forms.FileField(label='Select zip file', widget=forms.FileInput(attrs={'accept': '.zip'}))

关于django - 限制django表单文件上传的文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42274084/

相关文章:

python - 如何在 Django 中将 html 页面转换为 pdf

python - 错误 : Django 'ChoiceField' object has no attribute 'use_required_attribute'

html - Wordpress 的表单问题

php - 使用ffmpeg php时如何知道何时完成转换?

python - 没有名为 psycopg2 的模块

javascript - Django 对 Javascript 文件翻译的支持

MySQL 加载数据本地 Infile - 路径作为用户变量

ruby-on-rails - 使用没有 cookie 的设计 "rememberable"

javascript - 关于 HTML 表单验证我需要担心什么?

javascript - 表单提交后将获取变量附加到 url,无需新的隐藏输入