关于apache2 / mod_wsgi和collectstatic的Django

时间:2012-02-08 07:10:46

标签: django apache mod-wsgi django-staticfiles

我正在Ubuntu 11.04下在Apache上配置Django。我的媒体文件不可用。

的httpd.conf

Alias /robots.txt /home/i159/workspace/prod-shivablog/shivablog/robots.txt
Alias /favicon.ico /home/i159/workspace/prod-shivablog/shivalog/favicon.ico

AliasMatch ^/([^/]*\.css) /home/i159/workspace/prod-     shivablog/shivablog/site_media/static/css/$1

Alias /media/ /home/i159/workspace/prod-shivablog/shivablog/site_media/static/
Alias /static/ /home/i159/workspace/prod-shivablog/shivablog/site_media/static/

<Directory /home/i159/workspace/prod-shivablog/shivablog/static>
Order deny,allow
Allow from all
</Directory>

<Directory /home/i159/workspace/prod-shivablog/shivablog/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /home/i159/workspace/prod-shivablog/shivablog/deploy/wsgi.py
WSGIDaemonProcess local-shivablog.com python-path=/home/i159/workspace/prod-    shivablog/shivablog/:/home/i159/.envs/shivablog/python2.7/site-packages

<Directory /home/i159/workspace/prod-shivablog/shivablog>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>

网址

# Static files url.
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
                                   {'document_root': settings.MEDIA_ROOT}),
(r'^site_media/static/(?P<path>.*)$', 'django.views.static.serve',
                                {'document_root': settings.STATIC_ROOT}),

WSGI

import os, sys

sys.path.insert(0,  os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir,  os.pardir)))
sys.path.insert(0,  os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir)))

from django.core.handlers.wsgi import WSGIHandler
os.environ["DJANGO_SETTINGS_MODULE"] = "shivablog.settings"
application = WSGIHandler()

设置

MEDIA_ROOT = ''

MEDIA_URL = "/media/"

STATIC_ROOT = ''

STATIC_URL = "/site_media/static/"

如何提供我的媒体文件?配置是正确的?在collectstatic之后,所有静态和媒体文件都会收集到site_media/static。我应该从此目录(site_media/static)获取媒体文件吗?

1 个答案:

答案 0 :(得分:0)

您的urls.py文件中不应包含媒体或静态文件的条目。如果你至少将它们包装在一个只在调试模式下为真的原因。

https://docs.djangoproject.com/en/1.2/howto/static-files/

(r'^media/(?P<path>.*)$', 'django.views.static.serve',
                                   {'document_root': settings.MEDIA_ROOT}),
(r'^site_media/static/(?P<path>.*)$', 'django.views.static.serve',
                                {'document_root': settings.STATIC_ROOT}),

你的.htaccess文件还有以下几行指向同一个文件夹,我认为它们应该不同,因为你的静态文件不在你的媒体文件夹中。

Alias /media/ /home/i159/workspace/prod-shivablog/shivablog/site_media/static/
Alias /static/ /home/i159/workspace/prod-shivablog/shivablog/site_media/static/