Send a link

Django

Table of contents


Serve static files directly from Django

Changes to settings.py
MEDIA_ROOT = '/home/user/devel/mysite/static/'
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'

Apparently the example code in settings.py is not that good for this purpose, as you need to re-define ADMIN_MEDIA_PREFIX to something else than "/media/" (the default) if you want to use "/media/" for your static files or Django will mix the two URLs up and try to serve your media files from the admin dir.

Instead, simply use a different keyword for your static media files. I will use "static" from now on!

Changes to urls.py
from django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    )

Just append this at the bottom of your file, and it will only be active on sites with DEBUG set to true. My production servers have an Apache alias that takes care of /static for these kind of sites, and I even explicitly set the handler to None, to be sure Python/Django never gets the request.

NOTE: This should only be used during development, since both performance and security is questionable.

Apache configuration example

<VirtualHost *:80>

    ServerName django.confighell.com
    ServerAlias www.django.confighell.com
    ServerAdmin webmaster@confighell.com
    DocumentRoot /home/user/prod/htdocs
    ErrorLog /home/user/prod/logs/error.log
    CustomLog /home/user/prod/logs/access.log combined

    <Location />
        Options None
        Order allow,deny
        Allow from all

        SetHandler python-program
        PythonInterpreter custom-site-id
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE mysite.settings
        PythonOption django.root /mysite
        PythonDebug On
        PythonPath "['/home/user/prod'] + sys.path"
    </Location>

    <Location /static>
        SetHandler None
    </Location>

    Alias /static/ /home/user/prod/htdocs/static/

</VirtualHost>

Resources



  • + : A leading plus sign indicates that this word must be present in every object returned.
  • - : A leading minus sign indicates that this word must not be present in any row returned.
  • By default (when neither plus nor minus is specified) the word is optional, but the object that contain it will be rated higher.
  • < > : These two operators are used to change a word's contribution to the relevance value that is assigned to a row.
  • ( ) : Parentheses are used to group words into subexpressions.
  • ~ : A leading tilde acts as a negation operator, causing the word's contribution to the object relevance to be negative. It's useful for marking noise words. An object that contains such a word will be rated lower than others, but will not be excluded altogether, as it would be with the - operator.
  • * : An asterisk is the truncation operator. Unlike the other operators, it should be appended to the word, not prepended.
  • " : The phrase, that is enclosed in double quotes ", matches only objects that contain this phrase literally, as it was typed.

Menu