diff options
author | Nishanth Amuluru | 2011-01-08 11:20:57 +0530 |
---|---|---|
committer | Nishanth Amuluru | 2011-01-08 11:20:57 +0530 |
commit | 65411d01d448ff0cd4abd14eee14cf60b5f8fc20 (patch) | |
tree | b4c404363c4c63a61d6e2f8bd26c5b057c1fb09d /parts/django/docs/howto/static-files.txt | |
parent | 2e35094d43b4cc6974172e1febf76abb50f086ec (diff) | |
download | pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.gz pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.tar.bz2 pytask-65411d01d448ff0cd4abd14eee14cf60b5f8fc20.zip |
Added buildout stuff and made changes accordingly
--HG--
rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/djangorecipe-0.20-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/infrae.subversion-1.4.5-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/py-1.4.0-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/zc.buildout-1.5.2-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/dependency_links.txt
rename : profile/management/__init__.py => eggs/zc.recipe.egg-1.3.2-py2.6.egg/EGG-INFO/not-zip-safe
rename : profile/management/__init__.py => parts/django/Django.egg-info/dependency_links.txt
rename : taskapp/models.py => parts/django/django/conf/app_template/models.py
rename : taskapp/tests.py => parts/django/django/conf/app_template/tests.py
rename : taskapp/views.py => parts/django/django/conf/app_template/views.py
rename : taskapp/views.py => parts/django/django/contrib/gis/tests/geo3d/views.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/delete/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/files/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/invalid_models/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/m2m_signals/__init__.py
rename : profile/management/__init__.py => parts/django/tests/modeltests/model_package/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/management/commands/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/bash_completion/models.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/delete_regress/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/file_storage/__init__.py
rename : profile/management/__init__.py => parts/django/tests/regressiontests/max_lengths/__init__.py
rename : profile/forms.py => pytask/profile/forms.py
rename : profile/management/__init__.py => pytask/profile/management/__init__.py
rename : profile/management/commands/seed_db.py => pytask/profile/management/commands/seed_db.py
rename : profile/models.py => pytask/profile/models.py
rename : profile/templatetags/user_tags.py => pytask/profile/templatetags/user_tags.py
rename : taskapp/tests.py => pytask/profile/tests.py
rename : profile/urls.py => pytask/profile/urls.py
rename : profile/utils.py => pytask/profile/utils.py
rename : profile/views.py => pytask/profile/views.py
rename : static/css/base.css => pytask/static/css/base.css
rename : taskapp/tests.py => pytask/taskapp/tests.py
rename : taskapp/views.py => pytask/taskapp/views.py
rename : templates/base.html => pytask/templates/base.html
rename : templates/profile/browse_notifications.html => pytask/templates/profile/browse_notifications.html
rename : templates/profile/edit.html => pytask/templates/profile/edit.html
rename : templates/profile/view.html => pytask/templates/profile/view.html
rename : templates/profile/view_notification.html => pytask/templates/profile/view_notification.html
rename : templates/registration/activate.html => pytask/templates/registration/activate.html
rename : templates/registration/activation_email.txt => pytask/templates/registration/activation_email.txt
rename : templates/registration/activation_email_subject.txt => pytask/templates/registration/activation_email_subject.txt
rename : templates/registration/logged_out.html => pytask/templates/registration/logged_out.html
rename : templates/registration/login.html => pytask/templates/registration/login.html
rename : templates/registration/logout.html => pytask/templates/registration/logout.html
rename : templates/registration/password_change_done.html => pytask/templates/registration/password_change_done.html
rename : templates/registration/password_change_form.html => pytask/templates/registration/password_change_form.html
rename : templates/registration/password_reset_complete.html => pytask/templates/registration/password_reset_complete.html
rename : templates/registration/password_reset_confirm.html => pytask/templates/registration/password_reset_confirm.html
rename : templates/registration/password_reset_done.html => pytask/templates/registration/password_reset_done.html
rename : templates/registration/password_reset_email.html => pytask/templates/registration/password_reset_email.html
rename : templates/registration/password_reset_form.html => pytask/templates/registration/password_reset_form.html
rename : templates/registration/registration_complete.html => pytask/templates/registration/registration_complete.html
rename : templates/registration/registration_form.html => pytask/templates/registration/registration_form.html
rename : utils.py => pytask/utils.py
Diffstat (limited to 'parts/django/docs/howto/static-files.txt')
-rw-r--r-- | parts/django/docs/howto/static-files.txt | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/parts/django/docs/howto/static-files.txt b/parts/django/docs/howto/static-files.txt new file mode 100644 index 0000000..c3692d5 --- /dev/null +++ b/parts/django/docs/howto/static-files.txt @@ -0,0 +1,162 @@ +========================= +How to serve static files +========================= + +.. module:: django.views.static + :synopsis: Serving of static files during development. + +Django itself doesn't serve static (media) files, such as images, style sheets, +or video. It leaves that job to whichever Web server you choose. + +The reasoning here is that standard Web servers, such as Apache_, lighttpd_ and +Cherokee_, are much more fine-tuned at serving static files than a Web +application framework. + +With that said, Django does support static files **during development**. You can +use the :func:`django.views.static.serve` view to serve media files. + +.. _Apache: http://httpd.apache.org/ +.. _lighttpd: http://www.lighttpd.net/ +.. _Cherokee: http://www.cherokee-project.com/ + +.. seealso:: + + If you just need to serve the admin media from a nonstandard location, see + the :djadminopt:`--adminmedia` parameter to :djadmin:`runserver`. + +The big, fat disclaimer +======================= + +Using this method is **inefficient** and **insecure**. Do not use this in a +production setting. Use this only for development. + +For information on serving static files in an Apache production environment, +see the :ref:`Django mod_python documentation <serving-media-files>`. + +How to do it +============ + +Here's the formal definition of the :func:`~django.views.static.serve` view: + +.. function:: def serve(request, path, document_root, show_indexes=False) + +To use it, just put this in your :doc:`URLconf </topics/http/urls>`:: + + (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', + {'document_root': '/path/to/media'}), + +...where ``site_media`` is the URL where your media will be rooted, and +``/path/to/media`` is the filesystem root for your media. This will call the +:func:`~django.views.static.serve` view, passing in the path from the URLconf +and the (required) ``document_root`` parameter. + +Given the above URLconf: + + * The file ``/path/to/media/foo.jpg`` will be made available at the URL + ``/site_media/foo.jpg``. + + * The file ``/path/to/media/css/mystyles.css`` will be made available + at the URL ``/site_media/css/mystyles.css``. + + * The file ``/path/bar.jpg`` will not be accessible, because it doesn't + fall under the document root. + +Of course, it's not compulsory to use a fixed string for the +``'document_root'`` value. You might wish to make that an entry in your +settings file and use the setting value there. That will allow you and +other developers working on the code to easily change the value as +required. For example, if we have a line in ``settings.py`` that says:: + + STATIC_DOC_ROOT = '/path/to/media' + +...we could write the above :doc:`URLconf </topics/http/urls>` entry as:: + + from django.conf import settings + ... + (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', + {'document_root': settings.STATIC_DOC_ROOT}), + +Be careful not to use the same path as your :setting:`ADMIN_MEDIA_PREFIX` (which defaults +to ``/media/``) as this will overwrite your URLconf entry. + +Directory listings +================== + +Optionally, you can pass the ``show_indexes`` parameter to the +:func:`~django.views.static.serve` view. This is ``False`` by default. If it's +``True``, Django will display file listings for directories. + +For example:: + + (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', + {'document_root': '/path/to/media', 'show_indexes': True}), + +You can customize the index view by creating a template called +``static/directory_index.html``. That template gets two objects in its context: + + * ``directory`` -- the directory name (a string) + * ``file_list`` -- a list of file names (as strings) in the directory + +Here's the default ``static/directory_index.html`` template: + +.. code-block:: html+django + + <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> + <head> + <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> + <meta http-equiv="Content-Language" content="en-us" /> + <meta name="robots" content="NONE,NOARCHIVE" /> + <title>Index of {{ directory }}</title> + </head> + <body> + <h1>Index of {{ directory }}</h1> + <ul> + {% for f in file_list %} + <li><a href="{{ f }}">{{ f }}</a></li> + {% endfor %} + </ul> + </body> + </html> + +.. versionchanged:: 1.0.3 + Prior to Django 1.0.3, there was a bug in the view that provided directory + listings. The template that was loaded had to be called + ``static/directory_listing`` (with no ``.html`` extension). For backwards + compatibility with earlier versions, Django will still load templates with + the older (no extension) name, but it will prefer the + ``directory_index.html`` version. + +Limiting use to DEBUG=True +========================== + +Because URLconfs are just plain Python modules, you can use Python logic to +make the static-media view available only in development mode. This is a handy +trick to make sure the static-serving view doesn't slip into a production +setting by mistake. + +Do this by wrapping an ``if DEBUG`` statement around the +:func:`django.views.static.serve` inclusion. Here's a full example URLconf:: + + from django.conf.urls.defaults import * + from django.conf import settings + + urlpatterns = patterns('', + (r'^articles/2003/$', 'news.views.special_case_2003'), + (r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'), + (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive'), + (r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'news.views.article_detail'), + ) + + if settings.DEBUG: + urlpatterns += patterns('', + (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/media'}), + ) + +This code is straightforward. It imports the settings and checks the value of +the :setting:`DEBUG` setting. If it evaluates to ``True``, then ``site_media`` +will be associated with the ``django.views.static.serve`` view. If not, then the +view won't be made available. + +Of course, the catch here is that you'll have to remember to set ``DEBUG=False`` +in your production settings file. But you should be doing that anyway. |