CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=dc8f7f0966dac40eb95029e5; HttpOnly; Path=/; Secure
set-cookie: trac_session=a1e4accc9577ac94bb7c8bcc; expires=Wed, 22 Oct 2025 19:54:40 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Thu, 24 Jul 2025 19:54:40 GMT
x-served-by: cache-fra-etou8220109-FRA, cache-bom-vanm7210091-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753386880.404910,VS0,VE178
vary: Accept-Encoding
StreamingUpload – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Streaming Upload
Example app for patch in ticket #2070
First svn up -r4065
Then apply The patch
# settings.py MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'django.middleware.upload.UploadStateMiddleware', 'django.middleware.upload.StreamingUploadMiddleware', )
# models.py from django.db import models class SomeFile(models.Model): description = models.CharField(maxlength=255) file = models.FileField(upload_to='files')
# manipulators.py from models import SomeFile from django import oldforms as form from django.conf import settings class AddFileManipulator(forms.Manipulator): def __init__(self): self.fields = ( forms.TextField(field_name="description", length=30, maxlength=200, is_required=True), forms.FileUploadField(field_name="file", is_required=True) ) def do_html2python(self, new_data): new_new_data = {} new_new_data['file_path'] = os.path.join(settings.MEDIA_ROOT, filename) os.rename(new_data['file'], new_new_data['file_path']) new_new_data['description'] = new_data['description'] new_data = new_new_data def save(self, new_data): new_object = SomeField(**new_data) new_object.save() return new_object
# views.py from manipulators import AddFileManipulator def upload_file(request): manipulator = AddFileManipulator() if request.method == 'POST': # If data was POSTed, we're trying to create a new Place. new_data = request.POST.copy() new_data.update(request.FILES) # Check for errors. errors = manipulator.get_validation_errors(new_data) if not errors: # No errors. This means we can save the data! manipulator.do_html2python(new_data) new_object = manipulator.save(new_data) # Redirect to the object's "edit" page. Always use a redirect # after POST data, so that reloads don't accidently create # duplicate entires, and so users don't see the confusing # "Repost POST data?" alert box in their browsers. return HttpResponseRedirect("/showuploadedfile/%i/" % new_object.id) else: # No POST, so we want a brand new form without any data or errors. errors = new_data = {} # Create the FormWrapper, template, context, response. form = forms.FormWrapper(manipulator, new_data, errors) return render_to_response('add_file.html', {'form': form})
<!-- add_file.html --> <form enctype="multipart/form-data" method="post" action="/uploadfile/"> <p>Description: {{ form.description }} </p> <p>File: {{ form.file_file }} </p> </form>
Last modified
19 years ago
Last modified on Jan 7, 2007, 11:06:13 AM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.