5

I'm trying to upload my code on an apache server using mod_python. I have tried a lot but the server is not able to access my static files (all my images, js and css). Here are my Virtualhost settings:

<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
Alias /static/ /home/mysite/products/static/
#
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com
RewriteRule (.*) http://mysite.com$1 [R=301,L] 
#
DocumentRoot /home
<Directory /home/mysite/>
    SetHandler mod_python
    PythonHandler mod_python.publisher
    PythonDebug On
</Directory>
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

</VirtualHost>

my access log:

"GET /giftproducts/static/js/top.js HTTP/1.1" 404 1487 "http://xxx.xxx.xx.xxx/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
xxx.xxx.xx.xxx - - [23/Apr/2013:15:09:52 -0500] "GET /giftproducts/static/css/index.css HTTP/1.1" 404 1486 "http://xxx.xxx.xx.xxx/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"

(404-page not found-WHY!?)

settings.py:

import os
import sys
path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
MEDIA_ROOT = path + '/products/media/'
MEDIA_URL = '/media/'

PROJECT_ROOT = path
STATIC_ROOT = path + '/products/static/'
STATIC_URL = '/products/static/'

STATICFILES_DIRS = (
    path,

)

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_DIRS = (
path + '/products/templates',
) #this works, since it is loading the html

tried giving paths like "/static/x" and "{{ STATIC_URL }}x" but nothing works.

Any help on this would be great. Thanks.

UPDATE: in addition to what Glyn suggested below, I added these lines to my urls.py, and then it worked.

if settings.DEBUG:
urlpatterns += patterns('',
 (r'^static/(?P<path>.*)$', 'django.views.static.serve',         
 {'document_root': settings.STATIC_ROOT}),
)
user_2000
  • 1,103
  • 3
  • 14
  • 26
  • 1
    The recommended way to put on production a Django application is with `mod_wsgi` as it says in: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/ You should give it a try that way – Paulo Bu Apr 23 '13 at 20:50
  • 1
    thanks, I've got it working with mod_python, but can you please tell me why is mod_wsgi recommended (as per your experience), dont want to run in problems with mod_python later on – user_2000 Apr 23 '13 at 21:33
  • 1
    mod_python is kinda dead now. If your starting anything new use mod_wsgi, is also recommended by Django. – Glyn Jackson Apr 23 '13 at 21:43
  • That's the reason. Besides, the latest most updated Django documentation use and recommend `mod_wsgi` and is better to use that and not engage with some old non-supported module. – Paulo Bu Apr 24 '13 at 02:13

1 Answers1

4

1) Have you setup your static files correctly in your virtual host? I don't see them...

i.e.

    Alias /media/ /products/static
    Alias /static/ /products/static


    <Directory /products/static>
        Order allow,deny
        Allow from all
    </Directory>

2) In your templates always use {{ STATIC_URL }} to retrieve static files, it's best practice.

3) Add django.contrib.staticfiles and run the collectstatic management command

Glyn Jackson
  • 8,228
  • 4
  • 29
  • 52
  • 1
    Thanks a lot for the response. I tried some stuff and now it works fine. Here's what I did and what not: 1. added the alias you mentioned and the directory part as well 2. added this to my urls.py (that what actually got it working I think) if settings.DEBUG: urlpatterns += patterns('', (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), ) 3. didn't run collectstatic, and I'm giving path as "/static/x" instead of {{ STATIC_URL }}x, do I really need to do it since it doesn't seem to work with {{ STATIC_URL }} – user_2000 Apr 23 '13 at 21:27
  • 2
    @user_2000 I'm glad you got it working. If my answer help please accept. On another note: {{ STATIC_URL }}x and /static/x 'should' be the same when you view the output. If they are not then check your static paths. I always recommend to use {{ STATIC_URL }} over hardcoded, it will save you a lot of pain in the long run! – Glyn Jackson Apr 23 '13 at 21:41