[python] Apache不提供django管理靜態文件


Answers

那是因為你還沒有設置你的STATIC文件......

添加到設置:

STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'

然後運行“python manage.py collectstatic”

這將把所有文件放在STATIC_ROOT下STATIC_URL將服務...你不應該將Apache指向你的Python lib文件!

如果您還需要自己的應用程序特定的靜態文件,請設置“STATICFILES_DIRS”。

Question

讓我感謝社區的各位幫助我解決各種Django和Apache(帶有mod_wsgi)錯誤。 到目前為止,我已經詢問了5個相關問題,現在我越來越接近在生產網站上獲取我的內容了!

所以我知道有很多類似的問題,我已經閱讀了questions about on Django on serving static media files of questions

我讀了STATIC_URLSTATIC_ROOT ,(很快就會過時) ADMIN_MEDIA_PREFIX ,並在Apache配置中設置了Alias /media/ ... 我試圖逐個測試每個解決方案,但我無法正常工作。

這是我的管理網站現在的樣子

我也有一個奇怪的情況, 任何子域在我的服務器上工作。 例如,我試圖設置我的服務器,以便http://www.satoshi.example.com/允許我的正常(非Django)內容,而http://django.satoshi.example.com/將允許我的Django內容將被提供。 但目前任何子域名,無論是satoshi.example.com還是blahblahasdas.satoshi.example.com都在為我的Django文件提供服務(我知道因為我可以訪問兩個站點的/admin頁面,儘管它們將在不同的會話中)。

無論如何這裡是我的服務器上的文件運行CentOS (不確定是哪個版本), Apache 2.2.15Python 2.6.6django 1.3.1mod_wsgi 3.2

我將發布我認為最相關的文件和配置如下:

Apache每次重新啟動時都會拋出這些錯誤

[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [notice] SIGHUP received.  Attempting to restart
[Wed Feb 29 00:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored
[Wed Feb 29 01:45:36 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Feb 29 01:45:36 2012] [notice] Digest: done
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Compiled for Python/2.6.2.
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Runtime using Python/2.6.6.
[Wed Feb 29 01:45:36 2012] [notice] Apache/2.2.15 (Unix) mod_auth_pgsql/2.0.3 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.6 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations

這是/var/www/html/mysite/apache/apache_django_wsgi.conf ,它使用選項NameVirtualHost *:80加載到我的httpd.conf NameVirtualHost *:80

<VirtualHost *:80>
    ServerName django.satoshi.example.com
    ErrorLog "/var/log/httpd/django_error_log"

    WSGIDaemonProcess django
    WSGIProcessGroup django

    Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
    <Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/media">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>

    <Directory "/var/www/html/mysite">
        Order allow,deny
        Options Indexes
        Allow from all
        IndexOptions FancyIndexing
    </Directory>

    WSGIScriptAlias / "/var/www/html/mysite/apache/django.wsgi"

    <Directory "/var/www/html/mysite/apache">
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

這是/var/www/html/mysite/apache/django.wsgi

import os
import sys

paths = [
    '/var/www/html/mysite',
    '/var/www/html',
    '/usr/lib/python2.6/site-packages/',
]

for path in paths:
    if path not in sys.path:
        sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

最後這裡是/var/www/html/mysite/settings.py一部分

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'

# Additional locations of static files
STATICFILES_DIRS = ( 
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

如果你們需要任何其他文件,請告訴我。 提前致謝!




Related