让我感谢Stack Overflow社区的各位帮助我解决了各种Django和Apache(使用mod_wsgi)错误.到目前为止,我已经问了大约5个相关的问题,现在我越来越接近在生产网站上发布我的内容了!

所以我知道有很多类似的问题,我读过一篇bunch of questions about serving static media files on Django.

我读到了大约STATIC_URL,STATIC_ROOT,ADMIN_MEDIA_PREFIX(很快就会过时),并在Apache配置中设置了Alias /media/ ....我试着逐个测试每种解决方案,但什么都不能用.

这是我的管理站点现在的样子

我还有一个奇怪的例子,any个子域在我的服务器上工作.例如,我试图设置我的服务器,使http://www.satoshi.example.com/允许我的正常(非Django)内容,而http://django.satoshi.example.com/允许我的Django内容被服务.但目前任何子域,无论是satoshi.实例com或blahblahasdas.佐藤.实例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

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

如果你们还需要其他文件,请告诉我.提前谢谢!

推荐答案

我认为你应该改变一下:

Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"

致:

Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"

因为你有:

ADMIN_MEDIA_PREFIX = '/static/admin/'

Django相关问答推荐

Django 5.0.2:TypeError:获取切片后无法过滤查询

Django中的Sync_to_Async修饰器、异步视图

使用自定义模型注册后,Django无法登录

通过父模型查询子对象-Django

django re_path() 函数模式与包含该模式的 url 不匹配

如何使用邮箱确认码创建django注册

有什么方法可以自动设置 Debug True Django application

Django ORM:子查询上的文本聚合器

NoneType对象没有属性保存Django

防止 django 将_id附加到外键字段

如何在 PyCharm 中重命名 Django 元素?

Django,如何从模型表单的 Select 字段中删除空白 Select ?

如何在 Django 中向 ModelForm 添加外键字段?

在 Mac OS X 上安装支持 JPEG 的 PIL

如何使用 Requests 库执行 HTTP DELETE 请求

javascript 文件中的 Django {% static 'path' %}

如何在 django 中仅获取表的特定列?

想要在 Django 测试中禁用信号

AttributeError:ManyRelatedManager对象没有add属性?

django/文件上传权限