0

I have a django web application running on our apache2 production server using mod_python, but no static files are found (css,images ... )

All our static stuff is under /var/my.site/example/static

/var/my.site/example/static/
                           |-admin/
                                  |-css/
                                  |-img/     
                           |-css/
                           |-js/
                           |-img/

Now I thought I just could alias all requests to my static stuff like so:

This is the apache2 conf:

<VirtualHost  123.123.123:443>
    ... SSL stuff ...
    RewriteEngine On    
    ReWriteOptions Inherit 
    <Location "/example">
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE example.settings
        PythonPath "[ \
            '/home/me/Envs/ex/lib/python2.6/site-packages',\
            '/var/my.site',\
            '/home/me/Envs/ex/lib/python2.6/site-packages/django',\
            '/home/me/Envs/ex/lib/python2.6/site-packages/MySQLdb',\
            '/var/my.site/example',\
            '/var/my.site/example/static'] + sys.path"
        PythonDebug Off

    </Location>

    Alias /example/static /var/my.site/example/static    
    <Directory /var/my.site/example/static>
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

This is my settings.py

...
STATIC_ROOT = '/var/my.site'
STATIC_URL = '/example/static/'
STATICFILES_DIRS = (
    "/var/my.site/example/static",
)
...

There is no errors in the apache-error log. But here log from apache-secure_access.log

[09/Aug/2012:12:37:55 +0200] "GET /example/admin/ HTTP/1.1" 200 6694
[09/Aug/2012:12:37:55 +0200] "GET /example/static/css/base.css HTTP/1.1" 301 468
[09/Aug/2012:12:37:55 +0200] "GET /example/static/img/logo.png HTTP/1.1" 403 766
[09/Aug/2012:12:37:55 +0200] "GET /example/static/css/base.css/ HTTP/1.1" 500 756
[09/Aug/2012:12:37:55 +0200] "GET /example/static/admin/css/dashboard.css HTTP/1.1" 301 622

But this doesn't work and I'm not sure, if I even, is on the right track. It does work when I set DEBUG = True But that's just because django serves all the static files.

What am I doing wrong?

Does anyone know about a good tutorial or example?

7
  • does putting Alias /example/static /var/my.site/example/static before <Location "/example"> make a difference? Commented Aug 9, 2012 at 11:18
  • can you show ... django-stuff ...? Commented Aug 9, 2012 at 11:23
  • Thanks for taken your time to look at it! I've updated the question with my django-stuff Commented Aug 9, 2012 at 11:43
  • Try making it <Directory "/var/my.site/example/static"> Commented Aug 9, 2012 at 11:51
  • 3
    I know you said you're using mod_python, but I use mod_wsgi to do exactly what you're talking about. Here's the documentation that got me going: code.google.com/p/modwsgi/wiki/… Commented Aug 9, 2012 at 12:36

3 Answers 3

2

Try to eliminate the problem step-by-step.

Loading static files should work completely independently of Django. Try commenting out all lines relating to Django in your VirtualHost config. (Remember to reload Apache after changing the configuration)

If that works, it may be that you need to take more steps to avoid Django trampling over URLs in the same namespace (perhaps using SetHandler?).

If not, there's a more basic problem with your static files. If you can't resolve it, perhaps ServerFault can help?

Sign up to request clarification or add additional context in comments.

Comments

2

After @supervacuo suggestion that I strip down everything from django, I got apache to serve the static files and realized what was wrong.

The problem was that <Location "/example"> got priority over Alias /example/static. It didn't matter where I put the Alias (above or below the <Location> - tag).

To fix it I changed the STATIC_URL and STATIC_ROOT, than I could change the Alias not to interfere with the <Location> - tag

From:

STATIC_ROOT = '/var/my.site'
STATIC_URL = '/example/static/'

Alias /example/static /var/my.site/example/static

To:

STATIC_ROOT = '/var/my.site/example'
STATIC_URL = '/static/'

Alias /static /var/my.site/example/static   

Comments

0

The only problem I can see is with this:

"GET /example/static/css/base.css/ HTTP/1.1" 500 756

Since /base.css/ is not a valid link; it gets passed to django; and since it doesn't match a URL pattern it raises a 500. You should fix the template that has the errant /.

1 Comment

Actually, this is related to the 301 a couple of lines above it; it's Django's APPEND_SLASH setting kicking in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.