【问题标题】:Django Nginx is not serving wagtail admin css/js filesDjango Nginx 不提供 wagtail 管理 css/js 文件
【发布时间】:2019-02-18 19:27:12
【问题描述】:

我在一个使用 NGINX 和 Gunicorn 的域的 ubuntu 服务器上设置了我的 django wagtail。我的目录中 static 文件夹中的 CSS 和 JS 文件得到了正确的服务,但我不知道为什么 Wagtail Admin CSS/JS 文件没有得到服务。我假设这与 Wagtail 管理文件不在我的 CSS/JS 文件的静态文件夹中的事实有关。 我运行了 CollectStatic 并设置了 Debug=False。

Google chrome 报告在管理员 CSS/JS 上找不到 404 文件

NGINX 文件的一部分

server {
        listen 443 default_server;
        listen [::]:443 default_server;

        root /home/projects/stemletics/stemletics/mysite/mysite;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name domain.com www.domain.com;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /home/projects/stemletics/stemletics/mysite/mysite;
        }
        location / {
                include proxy_params;
                proxy_pass http://unix:/home/projects/stemletics/stemletics/mysite/mysite.sock;
        }

Base.py 的相关部分

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATICFILES_DIRS = [
    os.path.join(PROJECT_DIR, 'static'),
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'


# Wagtail settings

WAGTAIL_SITE_NAME = "mysite"

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'http://example.com'

Production.py

from .base import *

DEBUG = False
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY_STEMLETICS"]

try:
    from .local import *
except ImportError:
    pass

静态文件夹路径

root@django-manaland:/# cd home/projects/stemletics/stemletics/mysite/mysite/static/
root@django-manaland:/home/projects/stemletics/stemletics/mysite/mysite/static# lsbrand  css  fonts  img  js  scss

【问题讨论】:

  • 你配置不正确,我不认为你的静态目录是/home/projects/stemletics/stemletics/mysite/mysite。请从您的settings.py 发布相关部分。
  • 添加了要发布的请求信息
  • 您的location /static/ 应该指向静态目录,即/my/project/dir/static。如何提供您自己的 CSS/JS 文件是一个谜,但我认为这是因为您在 HTML 中手动编码了静态 URL(它们也指向错误的位置)。
  • 哈哈,我的 Nginx 文件太乱了,不过我现在完全理解了。我的 Nginx 文件中的根目录和静态位置都是错误的。谢谢!

标签: python django wagtail


【解决方案1】:

在您的 nginx 配置文件中,您需要指向您要从中提供静态资产的目录。乍一看,你所看到的看起来是正确的(虽然我不能 100% 确定你是否需要下面示例中的斜杠;我总是留下斜杠以防万一)

# your nginx site.conf file

# Site static media
location /static/ {
  # Use `pwd` to get this path, wherever your static assets are collected into
  alias /home/user/www/your_website/static_collected/; 
}

要获取该路径(从上面),请通过 ssh 进入您的服务器,cd 进入 Django 的 collectstatic 函数合并您的文件的目录,然后运行 ​​pwd。它会返回一条路径,并确保以斜杠结尾(斜杠一直对我有用)

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 1970-01-01
    • 2017-07-18
    • 2017-05-10
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2014-07-06
    相关资源
    最近更新 更多