将 Django Python 应用程序放入 Docker 容器后收到 404 错误消息

Getting 404 error messages after placing Django Python app in Docker Container

我正在尝试获取一些模板信息以显示在 Docker container 提供的网页上。它正在使用 uWSGI。模板的名称是 base.html

有 "decorations" 与位于 static directory 中的 base.html 文件关联。更具体地说,它位于 static/wforms/assets 目录中。 以下是 assets 目录在模板中的使用方式。

<link href="{% static 'wforms/assets/global/plugins/font-awesome/css/font-awesome.min.css' %}" rel="stylesheet" type="text/css" />
    <link href="{% static 'wforms/assets/global/plugins/simple-line-icons/simple-line-icons.min.css' %}" rel="stylesheet" type="text/css" />
    <link href="{% static 'wforms/assets/global/plugins/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" type="text/css" />

每当我进入使用base.html模板的页面时,装饰都找不到,结果网页一团糟。在 PyCharm 中一切正常。将所有内容移动到 Docker 容器中的目录结构时会发生此问题。下面是PyCharm.

中的目录结构

我这样启动容器:

docker run -it --rm -p 8888:8888  -v /home/dockcclubdjango/apps/wforms/assets:/code/backendworkproj/static/wforms/assets --name my-running-app my-python-app

仅供参考,/home/dockcclubdjango/apps/wforms/assets 确实存在并且包含正确的信息。

然后,我尝试转到网络主页。该页面出现了,但所有 "decorations" 都不存在。所以,页面看起来有些凌乱。像这样:

我查看页面的源代码,看到下面的内容。

<link href="/static/wforms/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css" rel="stylesheet" type="text/css" />  <<< got 404 error in log file
    <link href="/static/wforms/assets/global/plugins/morris/morris.css" rel="stylesheet" type="text/css" />                                  <<< got 404 error in log file
    <link href="/static/wforms/assets/global/plugins/fullcalendar/fullcalendar.min.css" rel="stylesheet" type="text/css" />                  <<< got 404 error in log file

我注意到找不到与 assets 目录关联的任何项目。 列出的每个项目在日志中都有一个 404 Error

所以,我尝试了以下方法以不同方式启动 Docker Container(如下所示),但它不起作用

docker run -it --rm -p 8888:8888  -v /home/dockcclubdjango/apps/wforms/assets:/static/wforms/assets --name my-running-app my-python-app

我开始查看 settings.py 文件,但不确定哪里出了问题。

settings.py 文件(减少以使其更简单 - 专注于 STATIC dirs

import os

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

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

STATIC_DIR = os.path.join(BASE_DIR, 'static')

MEDIA_DIR = os.path.join(BASE_DIR, 'media')

DEBUG = True

ROOT_URLCONF = 'backendworkproj.urls'

WSGI_APPLICATION = 'backendworkproj.wsgi.application'

SESSION_COOKIE_AGE = 10*60


STATIC_URL = '/static/'
STATIC_ROOT =  os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
     os.path.join(BASE_DIR, "static"),
]

STATIC_ROOT = "/var/www/cclub.com/static/"

MEDIA_ROOT = MEDIA_DIR

我看到了这个帖子

并进行了其中建议的更改,但仍然无法正常工作(页面上的 "decorations" 一直显示 404 错误。不确定下一步要看哪里。我在这里缺少什么?

TIA

更新

@PekosoG - 当我使用下面的建议时,当 运行 docker build -t my-python-app .

 ---> Running in fde638af597f
Traceback (most recent call last):
  File "/code/backendworkproj/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
    collected = self.collect()
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    for finder in get_finders():
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/finders.py", line 264, in get_finders
    yield get_finder(finder_path)
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/finders.py", line 277, in get_finder
    return Finder()
  File "/usr/local/lib/python3.6/site-packages/django/contrib/staticfiles/finders.py", line 66, in __init__
    "The STATICFILES_DIRS setting should "
django.core.exceptions.ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting

更新

恢复到旧设置(因为上面的解决方案没有解决问题)

我遇到了类似的问题,我是这样解决的:

设置文件

STATIC_URL = '/static/'
STATIC_FOLDER = 'static'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,STATIC_FOLDER),
]

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

并且当我 运行 manage.py 命令初始化时我添加了这个

python manage.py collectstatic -link --noinput

好的,没关系。我发现这就是为我解决问题的原因。不确定它是否是最佳选择 - 但是 - 它有效...

http://uwsgi-docs.readthedocs.io/en/latest/StaticFiles.html