python - 无法获取管理员工作的静态文件

标签 python django apache mod-wsgi

正如标题所说,所有 404 都带有:

Page not found (404)
Request Method:     GET
Request URL:    http://localhost/static/admin/css/base.css

Using the URLconf defined in djcms.urls, Django tried these URL patterns, in this order:

    ^admin/

The current URL, static/admin/css/base.css, didn't match any of these.

基本上,我只是按照 DjangoProject.com 上的教程进行操作,所以除了 Django 本身之外,这里没有什么复杂的事情发生。

我的文件夹结构如下:

root@jerkstore:/var/www/djcms# ls -lsa
total 36
4 drwxrwxr-x  6 myusername myusername 4096 Jul 11 11:49 .
4 drwxr-xr-x 10 myusername www-data  4096 Jul 10 16:34 ..
4 drwxrwxr-x  5 myusername myusername 4096 Jul 11 11:40 admin
4 drwxrwxr-x  2 myusername myusername 4096 Jul 11 15:38 djcms
4 -rwxr-xr-x  1 myusername myusername  248 Jul 10 16:34 manage.py
4 drwxrwxr-x  2 myusername myusername 4096 Jul 11 10:53 polls
4 -rw-rw-r--  1 myusername myusername  415 Jul 11 10:26 .project
4 -rw-rw-r--  1 myusername myusername  577 Jul 11 10:26 .pydevproject
4 drwxrwxr-x  2 myusername myusername 4096 Jul 11 11:43 static

root@jerkstore:/var/www/djcms# ls -lsa static/
total 12
4 drwxrwxr-x 2 myusername myusername 4096 Jul 11 11:43 .
4 drwxrwxr-x 6 myusername myusername 4096 Jul 11 11:49 ..
4 lrwxrwxrwx 1 myusername myusername   73 Jul 11 11:43 admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/

root@jerkstore:/var/www/djcms# ls -lsa static/admin/
total 20
4 drwxr-sr-x 5 root staff 4096 Jul 10 16:33 .
4 drwxr-sr-x 3 root staff 4096 Jul 10 16:33 ..
4 drwxr-sr-x 2 root staff 4096 Jul 10 16:33 css
4 drwxr-sr-x 3 root staff 4096 Jul 10 16:33 img
4 drwxr-sr-x 3 root staff 4096 Jul 10 16:33 js

注意:Apache 中启用了 FollowSymLinks

settings.py 的相关部分如下所示:

# 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: "/var/www/example.com/static/"
STATIC_ROOT = '/var/www/djcms/static'

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

# 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.
    #'/var/www/djcms/static',
)

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

ADMIN_MEDIA_PREFIX = '/static/admin/'

我已经在 settings.py 中尝试了 STATIC_ vars 的几种不同排列,但到目前为止没有任何效果,也没有任何其他我能够通过谷歌搜索到的内容。

最佳答案

看起来对 admin 应用程序静态文件的请求正在访问 Django 应用程序。我假设您的 URL 定义正确,但值得重新检查 Django 静态文件配置。

如图所示 in the documentation on static files :

Serving the files

In addition to these configuration steps, you’ll also need to actually serve the static files.

During development, this will be done automatically if you use runserver and DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

See Deploying static files for proper strategies to serve static files in production environments.

这意味着,您需要设置您的 Web 服务器(例如 Apache)以在 Django 外部提供静态文件(因此对该 URL 的请求甚至不应该到达 Django 应用程序)。

还要确保您已调用 collectstatic .

关于python - 无法获取管理员工作的静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604160/

相关文章:

python - 查找可能的唯一固定长度排列数的最有效方法?

python - SVR预测语法解释

windows - 如何在 Windows 上为 SSL 配置 Apache?

linux - debian apache http 服务器抛出错误的组名 nobody 错误

django - 如何避免 Django Forms 和 Django Rest Framework Serializers 中的代码重复?

linux - IIS 代理到 Apache 并通过域身份验证 howto

Python:生成器失败

python - 在这段代码中,Python 的 timeit(...) 方法实际计时的代码是什么?

django - 使用 django rest 框架在元数据中添加字段

django - 是否可以使用Django的QuerySet.values_list来引用属性?