I have a form in my django app where users can upload files.
How can i set a limit to the uploaded file size so that if a user uploads a file larger than my limit the form won't be valid and it will throw an error?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- File Upload of more than 4GB
- The current request is not a multipart request - S
- Django: Replacement for the default ManyToMany Wid
Just a short note on the snippet that was included in this thread:
It was very usefull, however it's including a few minor mistakes. More robust code should look like this:
There are just a few improvements:
First of all I'm detecting if the file field is empty (None) - without it, Django will cast an exception in web browser.
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.
Last but not least, the unicode 'u' prefix in ValidationError function.
Thank you very much for this snippet!
Another elegant solution with validators that does not hard-code the max file size is by using a class based validator:
and then, in your model, for example:
EDIT: here is the source code of
MaxValueValidator
for more details on this works.You can use this snippet formatChecker. What it does is
it lets you specify what file formats are allowed to be uploaded.
and lets you set the limit of file size of the file to be uploaded.
First. Create a file named formatChecker.py inside the app where the you have the model that has the FileField that you want to accept a certain file type.
This is your formatChecker.py:
Second. In your models.py, add this:
Then instead of using 'FileField', use this 'ContentTypeRestrictedFileField'.
Example:
You can change the value of 'max_upload_size' to the limit of file size that you want. You can also change the values inside the list of 'content_types' to the file types that you want to accept.
If someone is looking for a form
FileField
variant of @angelo solution then here it isThen create a form as
I want to thank all the folks who have provided various different solutions to this problem. I had additional requirements where I wanted to (a) do file length validation in JavaScript before submission, (b) do a second line of defense in-server validation in the
forms.py
, (c) keep all hard-coded bits including end-user messages informs.py
, (d) I wanted myviews.py
have as little file-related code as possible, and (d) upload the file information to my database since these are small files that I want to only serve to logged in users and instantly delete when theMeal
model items are deleted (i.e. so just dropping them in /media/ is not sufficient).First the model:
Then you need a form that both does the in-server validation and the pre-save conversion from
InMemoryUploadedFile
tobytes
and grabbing theContent-Type
for later serving.In the template, add this code (adapted from a previous answer):
Here is the view code that handles both Create and Update:
This is a very simple view that makes sure that request.FILES is passed in during the creation of the instance. You could almost use the generic CreateView if it would (a) use my form and (b) pass request.files when making the model instance.
Just to complete the effort, I have the following simple view to stream the file:
This does not force users to be logged in, but I omitted that since this answer is already too long.