Demo project

The demo/ folder holds a demo project to illustrate django-downloadview usage.

Browse demo code online

See demo folder in project’s repository [1].

Deploy the demo

System requirements:

  • Python [2] version 2.6 or 2.7, available as python command.

    Note

    You may use Virtualenv [3] to make sure the active python is the right one.

  • make and wget to use the provided Makefile.

Execute:

git clone git@github.com:benoitbryon/django-downloadview.git
cd django-downloadview/
make demo

It installs and runs the demo server on localhost, port 8000. So have a look at http://localhost:8000/

Note

If you cannot execute the Makefile, read it and adapt the few commands it contains to your needs.

Browse and use demo/demoproject/ as a sandbox.

Base example provided in the demo

In the “demoproject” project, there is an application called “download”.

demo/demoproject/settings.py:

STATIC_URL = '/static/'


# Applications.
INSTALLED_APPS = (
    # Standard Django applications.
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # The actual django-downloadview demo.
    'demoproject',
    'demoproject.download',  # Sample standard download views.
    'demoproject.nginx',  # Sample optimizations for Nginx.
    # For test purposes. The demo project is part of django-downloadview

This application holds a Document model.

demo/demoproject/download/models.py:

from django.db import models


class Document(models.Model):
    """A sample model with a FileField."""
    slug = models.SlugField(verbose_name='slug')
    file = models.FileField(verbose_name='file', upload_to='document')

Note

The storage is the default one, i.e. it uses settings.MEDIA_ROOT. Combined to this upload_to configuration, files for Document model live in var/media/document/ folder, relative to your django-downloadview clone root.

There is a download view named “download_document” for this model:

demo/demoproject/download/urls.py:

# coding=utf8
"""URL mapping."""
from django.conf.urls import patterns, url


urlpatterns = patterns(
    'demoproject.download.views',
    # Model-based downloads.
    url(r'^document/(?P<slug>[a-zA-Z0-9_-]+)/$',
        'download_document',
        name='document'),
    # Storage-based downloads.
    url(r'^storage/(?P<path>[a-zA-Z0-9_-]+\.[a-zA-Z0-9]{1,4})$',
        'download_fixture_from_storage',
        name='fixture_from_storage'),
    # Path-based downloads.
    url(r'^hello-world\.txt$',
        'download_hello_world',
        name='hello_world'),
    url(r'^path/(?P<path>[a-zA-Z0-9_-]+\.[a-zA-Z0-9]{1,4})$',
        'download_fixture_from_path',
        name='fixture_from_path'),
    # URL-based downloads.
    url(r'^http/readme\.txt$',
        'download_http_hello_world',
        name='http_hello_world'),
    # Generated downloads.
    url(r'^generated/hello-world\.txt$',
        'download_generated_hello_world',
        name='generated_hello_world'),
)

As is, Django is to serve the files, i.e. load chunks into memory and stream them.

Table Of Contents

Previous topic

django-downloadview

Next topic

Installation

This Page