将Django Python应用程序放入Docker Contain后得到404条错误消息

2024-10-02 08:14:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

base.html文件相关联的“装饰”位于static directory中。更具体地说,它位于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中的目录结构。在

enter image description here

我这样启动容器:

^{pr2}$

作为参考,/home/dockclubdjango/apps/wforms/assets确实存在,并且其中包含正确的信息。在

然后,我试图转到web主页。页面显示,但所有的“装饰”都不存在。所以,页面看起来有点混乱。像这样:

enter image description here

我看一下这个页面的源代码并看到下面的内容。在

<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文件,但不确定是什么地方出错了。在

设置.py文件(减少以使其更简单-关注静态目录

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

我看到了这条线 Docker Django 404 for web static files, but fine for admin static files

并做出了建议中的修改,但仍然没有起作用(页面上的“装饰”不断出现404个错误)。不知道下一步该去哪里。我错过了什么?在

TIA公司

更新

@PekosoG-当我使用下面的建议时,我在运行docker build-t my python应用程序时得到以下结果。

 ---> 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

更新

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


Tags: pathdjangoinpyoslibusrlocal
2条回答

我有一个类似的问题,我是这样解决的:

设置文件

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

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

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

当我运行管理.py初始化命令我添加了这个

^{pr2}$

好吧,没关系。我发现这就是我解决问题的原因。不知道这是否是最好的选择-但是-它有效。。。在

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

相关问题 更多 >

    热门问题