首页 > 解决方案 > 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()

标签: pythondjango

解决方案


    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

推荐阅读