Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just a short note on the snippet that was included in this thread:</p> <blockquote> <p>Take a look at this snippet: <a href="http://www.djangosnippets.org/snippets/1303/" rel="noreferrer">http://www.djangosnippets.org/snippets/1303/</a></p> </blockquote> <p>It was very usefull, however it's including a few minor mistakes. More robust code should look like this: </p> <pre><code># Add to your settings file CONTENT_TYPES = ['image', 'video'] # 2.5MB - 2621440 # 5MB - 5242880 # 10MB - 10485760 # 20MB - 20971520 # 50MB - 5242880 # 100MB - 104857600 # 250MB - 214958080 # 500MB - 429916160 MAX_UPLOAD_SIZE = "5242880" #Add to a form containing a FileField and change the field names accordingly. from django.template.defaultfilters import filesizeformat from django.utils.translation import ugettext_lazy as _ from django.conf import settings def clean_content(self): if content != None: content = self.cleaned_data['content'] content_type = content.content_type.split('/')[0] if content_type in settings.CONTENT_TYPES: if content._size &gt; int(settings.MAX_UPLOAD_SIZE): raise forms.ValidationError(_(u'Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size))) else: raise forms.ValidationError(_(u'File type is not supported')) return content </code></pre> <p>There are just a few improvements:</p> <p>First of all I'm detecting if the file field is empty (None) - without it, Django will cast an exception in web browser.</p> <p>Next is type casting in int(settings.MAX_UPLOAD_SIZE), because that setting value is a string. Strings cannot be used for comparing with numbers.</p> <p>Last but not least, the unicode 'u' prefix in ValidationError function.</p> <p>Thank you very much for this snippet!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload