apache server cant find static files in Django project

做~自己de王妃 提交于 2019-12-23 15:54:17

问题


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}),
)

回答1:


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



来源:https://stackoverflow.com/questions/16178636/apache-server-cant-find-static-files-in-django-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!