2012-02-08 96 views
0

我在Ubuntu 11.04下的Apache上配置Django。我的媒體文件不可用。apache2上的Django/mod_wsgi和collectstatic

的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)?

+0

你的Apache配置看起來大致正確。你遇到了什麼錯誤?你有沒有檢查你的Apache日誌? – 2012-02-08 08:24:02

+0

我有一個錯誤403 - 服務器上沒有這個網址。 – I159 2012-02-08 10:38:27

回答

0

對於媒體文件或靜態文件,您的urls.py文件中不應包含條目。如果你至少把它們包裝在只有在調試模式= True的情況下才能做到的原因。

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/