python - Django 只允许上传指定的文件扩展名
问题描述
我想允许 django 文件 modol 只允许用户上传指定的扩展名(mp4、mpg 等....)
我该怎么做 ?
模型.py:
class Video(models.Model):
my_video = models.FileField(upload_to='uploaded_video/')
title = models.CharField(max_length=180)
date = models.DateTimeField(auto_now=True)
视图.py
def upload(request):
if request.method == 'POST':
get_file = request.FILES['up_video']
orginal_file_name = get_file.name
fs = FileSystemStorage()
fs.save(orginal_file_name, get_file)
form = VideoForm(request.POST, request.FILES)
form.save()
解决方案
if request.method == 'POST':
f = request.FILES['file']
if f.name=='':
return '<h2>Select a mp4 file to check</h2>'
elif f.name.split(".")[1]=='mp4':
# some code
推荐阅读
- c++ - 插入或 push_back 到 std::vector 的末尾?
- lattice-diamond - Lattice Diamond 中的安全程序功能
- java - JodaTime:如何从 LocalTime 获取格式为“HH:mm Z”的字符串表示形式
- php - Laravel 广播认证不使用标准用户认证
- php - 从文件中读取环境变量
- amazon-web-services - 处理不需要/旧/陈旧 AWS Athena 分区的正确方法
- powershell - 如何在文件夹中查找字符串并使用 PowerShell 返回值?
- c# - 有没有办法从 SQL 语句中获取列名?
- javascript - 为什么画布上的矩形会移位?
- maven - 如何将 Cytoscape 画布集成到 JavaFX Maven 应用程序中?