Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:43:14+00:00 2026-06-01T21:43:14+00:00

I have been asking questions about this subject a lot recently . i created

  • 0

I have been asking questions about this subject a lot recently.

i created my upload form

models.py

from django.db import models
from app.extra import ContentTypeRestrictedFileField

class upload(models.Model):
    """ upload """
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=250)
    file = ContentTypeRestrictedFileField(
        upload_to='/media/videos,'
        content_types=['video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov'],
        max_upload_size=104857600
    )
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)

    def __unicode__(self):
        return self.name

form.py

from django.db.models import FileField
from django.forms import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _

class ContentTypeRestrictedFileField(FileField):
    """
    Same as FileField, but you can specify:
        * content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
        * max_upload_size - a number indicating the maximum file size allowed for upload.
            2.5MB - 2621440
            5MB - 5242880
            10MB - 10485760
            20MB - 20971520
            50MB - 5242880
            100MB 104857600
            250MB - 214958080
            500MB - 429916160
    """
    def __init__(self, content_types=None,max_upload_size=104857600, **kwargs):
        self.content_types = kwargs.pop('video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov')
        self.max_upload_size = max_upload_size

        super(ContentTypeRestrictedFileField, self).__init__(**kwargs)


    def clean(self, *args, **kwargs):        
        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)

        file = data.file
        try:
            content_type = file.content_type
            if content_type in self.content_types:
                if file._size > self.max_upload_size:
                    raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
            else:
                raise forms.ValidationError(_('Filetype not supported.'))
        except AttributeError:
            pass        

        return data

        from south.modelsinspector import add_introspection_rules
        add_introspection_rules([], ["^app\.extra\.ContentTypeRestrictedFileField"])

and add this line is settings.py

FILE_UPLOAD_MAX_MEMORY_SIZE = 157286400 # 157286400 bytes = 150 MB

i was told to use this snippet, and i found this snippet

But there’s an issue that i didn’t see, a djangosnippets user say

If you're ok with letting people use up all your bandwidth for uploading 1GB 
files to your servers just to delete them as soon as the upload finishes, 
sure it's a great solution.

see this question. That question is about asp.net, i use django, So how to detect the file size of a video before that you upload it, in django

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:43:17+00:00Added an answer on June 1, 2026 at 9:43 pm

    You need to cut off HTTP upload before its hit Django and you do this usually on your front-end web server which could be Apache, Nginx or anything so this question is not Django specific.

    However the ultimate solution is to use HTML5 Javascript File API to read the file size on the client end and preventing the user to hit <form> submit button if he/she chooses too large file:

    https://developer.mozilla.org/en/DOM/File.size

    Because older browsers do not support both methods, you still need to fall back webserver cut and assume Javascript validation is not executed on every browser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been asking a lot of questions about a project I have been
I know there have been a million questions asking this, but mine is different.
Hey guys, lately I have been asking quite a few questions about memory management
I have been asking in previous questions about how to get multiple string inputs
I'm really sorry, I realize there have been several questions asked about cocos2d touch
I have been struggling to create a django form which users can fill and
I know there have been many questions asking for help converting URLs to clickable
Alright as I have been asking the last couple days and inching closer and
Have been working on this question for a couple hours and have come close
Have been reading about async and tasks and been attempting to convert the CopyFileEx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.