python - Django Nginx 不提供 wagtail 管理 css/js 文件

标签 python django wagtail

我在 ubuntu 服务器上设置了 django wagtail,并使用 NGINX 和 Gunicorn 进行域操作。我的目录中 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'

生产.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

最佳答案

在您的 nginx conf 文件中,您需要指向要从中提供静态 Assets 的目录。乍一看,您所拥有的看起来是正确的(尽管我不能 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。它将返回一个路径,并确保以尾部斜杠结束它(斜杠一直对我有用)

关于python - Django Nginx 不提供 wagtail 管理 css/js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52324240/

相关文章:

python - kubernetes.client.rest.ApiException:(401)原因:未经授权;创建/删除GKE Pane 的IAM

python - 如何在 Python 中用空格将字符串填充到固定长度?

django - S3 Lambda触发器不会为每个文件上传触发

python - 鹡鸰安装 : Wagtail command not found

python - Wagtail 'View live' 按钮在使用 id 作为 slug 时创建页面后提供错误的 url

python - 重复/连续 python 服务器和客户端

扩展构建器模式的 Pythonic 方式

Django Rest 框架 : Without Model

python - 组织 __init__.py 文件时循环导入错误

python - 使用 django-nose 但不使用 Django 的 DiscoverRunner 运行测试时出现重复键错误