1

需要明确的是,确实会出现调试工具栏。我的问题是,当我启用调试工具栏时,我的所有 javascript 和 css 文件都无法加载。

我的页面会丢失所有格式,但仍会加载内容。我正在使用 chrome,它在控制台下打印如下内容:

加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/stylesheets/loggedin.css?h=201e99加载资源失败:服务器响应状态为404(未找到) http://127.0.0.1:8000/static/stylesheets/estimates.tabs.css?h=64c8f9加载资源失败:服务器响应状态为 404(未找到) http://127.0 .0.1:8000/static/js/jquery.console-log.js加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/theme/jquery-ui .custom.css加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/js/jquery-ui-timepicker-addon.js?h=7e5e4a加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/js/jquery.if.min.js?h=e69918加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static/js/base.js?h=4b783f加载资源失败:服务器响应状态为 404(未找到) http:/ /127.0.0.1:8000/static/stylesheets/estimates.summary.show.css?h=0dd5d5加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static /js/jquery.floatThead.min.js?h=7f4ca5加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/stylesheets/reset.css?h =fbd5ac加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/js/estimates.section.sortable.js?h=2ea052加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static/stylesheets/loggedin.css?h=201e99加载资源失败:服务器响应状态为 404(未找到) http:/ /127.0.0.1:8000/static/stylesheets/estimates.tabs.css?h=64c8f9加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static/stylesheets /estimates.summary.show.css?h=0dd5d5加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static/stylesheets/reset.css?h=fbd5ac加载资源失败:服务器响应状态为 404(未找到) http://127.0.0.1:8000/static/ stylesheets/base.css?h=fcefb2加载资源失败:服务器响应状态为 404 (NOT FOUND) http://127.0.0.1:8000/static/theme/jquery-ui.custom.css加载失败资源:服务器响应状态为 404(未找到)

我正在使用 debug_toolbar 1.3.2、django1.5.0 和 python 2.7.6

我的项目结构有

project_dir
  project
  settings
    settings.py
  static
    images
    js
    scss
    stylesheets

在我的设置文件中:

DEBUG = True
DEBUG_TOOLBAR = True 

#....middleware
#....other Installed apps
cd_up = lambda x: os.path.split(x)[0]
SETTINGS_DIR = dirname(os.path.abspath(__file__))
APP_DIR = cd_up(SETTINGS_DIR)
PROJ_DIR = cd_up(APP_DIR)

MEDIA_ROOT = '/tmp'
MEDIA_URL = ''
STATIC_URL = '/static/'
STATIC_ROOT = '{}/static/'.format(APP_DIR)

if DEBUG_TOOLBAR:
  INSTALLED_APPS += ('django.contrib.staticfiles','debug_toolbar',)
  MIDDLEWARE_CLASSES += debug_toolbar.middleware.DebugToolbarMiddleware',)
  INTERNAL_IPS = ('127.0.0.1',)
  DEBUG_TOOLBAR_PANELS = (
    #'debug_toolbar.panels.version.VersionDebugPanel',
    #'debug_toolbar.panels.timer.TimerDebugPanel',
    #'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
    #'debug_toolbar.panels.headers.HeaderDebugPanel',
    #'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
    #'debug_toolbar.panels.template.TemplateDebugPanel',
    'debug_toolbar.panels.sql.SQLDebugPanel',
    #'debug_toolbar.panels.signals.SignalDebugPanel',
    #'debug_toolbar.panels.logger.LoggingPanel',
  )

自然,我无法发送任何 ajax 请求并将 DEBUG_TOOLBAR 设置为 false 可以解决所有问题。

这是一个精简版,但如果需要任何其他信息,我会更新。抱歉,我对 SO 还是有点陌生​​。谢谢

更新:运行 ./manage.py runserver --insecure 时出现 404 错误

否则资源失败并显示 net::ERR_NETWORK_CONNECTION_RESET 消息

4

2 回答 2

1

我不确定发生了什么,但这里有几点需要检查:

  1. 当 DEBUG_TOOLBAR 为 false 时,是否django.contrib.staticfiles在 INSTALLED_APPS 中?如果已添加,则此行

    INSTALLED_APPS += ('django.contrib.staticfiles','debug_toolbar',)

    可能会导致重复的应用名称。它应该不会导致任何问题,但您可以尝试删除重复项。

  2. 您确定 DEBUG 设置为 True 吗?当 DEBUG 为 False 时,开发服务器拒绝提供静态文件(CONNECTION RESET?),如果添加 --insecure 选项,它会提供静态文件。这和你描述的一样。

  3. 你是否覆盖了urls.py中“/static/”的url映射?

  4. 您是否在虚拟机中启动此服务器?如果是这样,您可以尝试使用以下命令启动服务器:

    python manage.py runserver 0.0.0.0:8000

    并将虚拟机的 IP 地址添加到 INTERNAL_IP。

于 2015-07-16T17:05:16.717 回答
1

我认为当您使用虚拟环境时会发生这种情况。问题是那里安装了 Django 包并且项目无法访问这些文件。您可以通过在控制台中运行(基于您的 python 版本)python3 manage.py collectstatic来解决这个问题。这就是我最终解决此问题的方法。希望对你的案子有帮助

于 2020-08-13T10:31:24.680 回答