You can place this snippet in management/commands/ and have it upload files from disk into objects in your database.
Usage:
```
python manage.py upload_file_to_model myapp mymodel myfield 1 field_name /some/file/path
```
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully.
You need to have PILLOW installed for this to work.
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully.
This validator works well with FileField form fields and can validate that an uploaded file has an acceptable mimetype. Place this snippet in your app's `validators.py`.
Requirements:
This snippet uses [python-magic](https://github.com/ahupp/python-magic). To install:
pip install python-magic
Usage (in forms.py):
from validators import MimetypeValidator
class MyForm(forms.Form):
file = forms.FileField(
allow_empty_file=False,
validators=[MimetypeValidator('application/pdf')],
help_text="Upload a PDF file"
)
Upload an image with file-uploader from http://github.com/valums/file-uploader
and save it to disk with the snippet and also to database if you want to
This snippet in the one to improve the method described in [http://djangosnippets.org/snippets/679/](http://djangosnippets.org/snippets/679/). It uses some jquery extensions to make the task more easier: **jquery.timers.js**, **jquery.progressbar.js**, **jquery.form.js**.
Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/)
Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)
A Django Form for creating a browser-based upload form that pushes files to Amazon S3. See http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1434
In-browser testing frameworks (I'm using [Windmill](http://www.getwindmill.com/)) have trouble testing file uploads because javascript's security policy prevents them from setting the value of file input fields. Instead the tests must issue some sort of "fake" file upload request, but implementing this on an ad-hoc basis quickly gets ugly.
This middleware is designed to support fake file uploads as transparently and as thoroughly as possible. For example, it is careful to properly trigger any file upload handlers so that things like upload progress reporting will work correctly. It can also simulate a slow file upload by sleeping between reads from the file.
From the client-side point of view, each input field of type "file" has a similarly-named hidden field automatically prepended. Test scripts can simply set the value of this hidden field to trigger a fake upload, rather than having to set the value of the file input field itself.
This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.
This script is an adaptation from http://www.djangosnippets.org/snippets/678/ . Here, it doesnt use the cache middleware but relies on sessions.
The script set a session cookie to identify the upload and track it to make it available for a progress bar like this one : http://www.djangosnippets.org/snippets/679/ . Note the progress bar cannot work with development server as it is single-threaded. Tested with apache/mod_python and mod_wsgi.
any comments appreciated ;)
This snippet is an example of an ajax progress bar (using jquery) that you might use in conjunction with <http://www.djangosnippets.org/snippets/678/>.
1. Generates a uuid and adds X-Progress-ID to the forms action url.
2. Adds the progress bar to the page. (you'll have to add some css styling for this)
3. Makes period ajax requests to update the progress bar.
Ticket [#2070](http://code.djangoproject.com/ticket/2070) allows you to create your own file upload handlers. Here's an example handler that tracks a file's upload so you might display a progress meter after form submission.
The snippet has two parts, the upload handler which tracks progress, and an upload_progress view used to report back to the browser.
The upload handler uses [django's cache framework](http://www.djangoproject.com/documentation/cache/#the-low-level-cache-api) to store the data, which can then be retrieved by the view to send back to the browser.
Note: Your form's http post request must have a query parameter (X-Progress-ID) sent along with it, which should contain a unique key to identify the upload. That key will also be sent with your ajax requests to the upload_progress view to retrieve the progress data.
Setup: place the UploadProgressCachedHandler anywhere you like on the python path, and add to your settings.py:
from django.conf import global_settings
FILE_UPLOAD_HANDLERS = ('path.to.UploadProgressCachedHandler', ) + \
global_settings.FILE_UPLOAD_HANDLERS
Set up the upload_progress view in any of your apps along with a corresponding entry in your urlconf.
Here's some javascript example code to make the ajax requests and display the progress meter: <http://www.djangosnippets.org/snippets/679/>
.