如何使用FileFields限制FileForms的文件上传文件类型? [英] How to limit file types on file uploads for ModelForms with FileFields?

查看:413
本文介绍了如何使用FileFields限制FileForms的文件上传文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将Django ModelForm上的FileField限制为PDF和Word文档。我已经googled的答案都是创建一个单独的文件处理程序,但我不知道如何在一个ModelForm的上下文中这样做。我可以使用settings.py中的设置来限制上传文件类型?

My goal is to limit a FileField on a Django ModelForm to PDFs and Word Documents. The answers I have googled all deal with creating a separate file handler, but I am not sure how to do so in the context of a ModelForm. Is there a setting in settings.py I may use to limit upload file types?

推荐答案

我使用clean_ [ your_field]方法。您可以在settings.py中设置可接受的文件扩展名单,以防止您的干净的方法,但没有内置的settings.py限制上传类型。

I handle this by using a clean_[your_field] method on a ModelForm. You could set a list of acceptable file extensions in settings.py to check against in your clean method, but there's nothing built-into settings.py to limit upload types.

例如,Django-Filebrowser采用在settings.py中创建可接受文件扩展名单的方法。

Django-Filebrowser, for example, takes the approach of creating a list of acceptable file extensions in settings.py.

希望能帮助您。

这篇关于如何使用FileFields限制FileForms的文件上传文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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