【问题标题】:why admin panel of django appears different in production version?为什么 django 的管理面板在生产版本中出现不同?
【发布时间】:2017-12-27 13:46:53
【问题描述】:

首先,我不知道这只是我的问题还是其他人也有。我的 django admin 的生产版本(我在我的网站中使用它)看起来与我在个人计算机上午餐时看到的不同。

您可以在两张图片中看到差异:

local version (127.0.0.1:8000)

production version(我的网站)

我想说的一点:

我的settings.py

STATIC_ROOT = '/home/django/django_project/django_project/static'
STATIC_URL = '/static/'

关于路径:

root@myuser:/home/django/django_project/django_project/static# ls
admin  css  fa  fonts  img  js

运行管理面板时的控制台日志显示与管理面板相关的静态文件(如 jquery.js)无法正确呈现。

mywebsite.com/:12 GET https://mywebsite.com/static/admin/js/vendor/jquery/jquery.js net::ERR_ABORTED
mywebsite.com/:18 GET https://mywebsite.com/static/admin/js/vendor/xregexp/xregexp.js net::ERR_ABORTED
mywebsite.com/:199 GET https://mywebsite.com/static/admin/js/change_form.js net::ERR_ABORTED
mywebsite.com/:208 GET https://mywebsite.com/static/admin/js/prepopulate_init.js net::ERR_ABORTED
mywebsite.com/:12 GET https://mywebsite.com/static/admin/js/vendor/jquery/jquery.js 404 (Not Found)
jquery.init.js:7 Uncaught ReferenceError: jQuery is not defined
    at jquery.init.js:7
(anonymous) @ jquery.init.js:7
actions.js:4 Uncaught TypeError: Cannot read property 'fn' of undefined
    at actions.js:4
    at actions.js:144
(anonymous) @ actions.js:4
(anonymous) @ actions.js:144
prepopulate.js:2 Uncaught TypeError: Cannot read property 'fn' of undefined
    at prepopulate.js:2
    at prepopulate.js:39
(anonymous) @ prepopulate.js:2
(anonymous) @ prepopulate.js:39
mywebsite.com/:18 GET https://mywebsite.com/static/admin/js/vendor/xregexp/xregexp.js net::ERR_ABORTED
mywebsite.com/:200 GET https://mywebsite.com/static/admin/js/change_form.js net::ERR_ABORTED
mywebsite.com/:209 GET https://mywebsite.com/static/admin/js/prepopulate_init.js 404 (Not Found)
DateTimeShortcuts.js:277 Uncaught TypeError: django.jQuery is not a function
    at Object.addCalendar (DateTimeShortcuts.js:277)
    at init (DateTimeShortcuts.js:45)
addCalendar @ DateTimeShortcuts.js:277
init @ DateTimeShortcuts.js:45
/missing-admin-media-prefix/img/icon_calendar.gif:1 GET https://mywebsite.com/missing-admin-media-prefix/img/icon_calendar.gif 404 (Not Found)
Image (async)
HTMLElement.appendChild @ VM155:323
addCalendar @ DateTimeShortcuts.js:224
init @ DateTimeShortcuts.js:45

web服务器是nginx,配置文件:

location /static {
    alias /home/django/django_project/django_project/static;
}

# Proxy the static assests for the Django Admin panel
location /static/admin {
   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
}

我也把第二个地址改成​​了:

alias /home/django/django_project/django_project/static/admin;

但仍然无法正常工作。 (我使用数字海洋作为托管)

  • 我没有编辑管理模板
  • 我在生产版本中使用了collectstatic 命令
  • 用不同的浏览器检查过
  • django 在服务器和本地的版本都是 1.11

那么在collectstatic之后我还需要做什么吗?

【问题讨论】:

  • 你是对的,静态文件有问题。但很难告诉你是什么,因为这取决于你如何部署它。还要检查你的浏览器调试器,看看哪些请求实际上失败了。
  • 你能更新更多信息吗?查看您的 settings.py 静态文件配置并了解您如何部署它会很有帮助。
  • @Daniel 我添加了一些额外的信息。
  • @KlausD。它无法从管理静态文件加载一些文件,如 jquery.js。但我不知道原因。
  • 很好,托管怎么样?是 nginx 吗?它是如何配置的?发布访问管理站点时浏览器控制台中显示的内容。

标签: python django django-admin


【解决方案1】:

collectstatic 命令将静态文件从管理站点和其他包复制到您的静态目录,因此您不需要问题中显示的第二个 location 块。

也改变

location /static {
    alias /home/django/django_project/django_project/static;
}

对于

location /static/ {
    root /home/django/django_project/django_project;
}

【讨论】:

    【解决方案2】:

    是的,现在您需要为您在 collectstatic 命令后创建的 static/admin 目录设置权限。在项目的根目录中(在生产环境中),运行以下命令。

    sudo chown -R 用户:www-data 静态

    其中 user = 您当前的系统用户。

    【讨论】:

    • 感谢您的回复,但我认为这不是解决方案,我对静态和管理文件夹都这样做了,但没有奏效。我自己的静态文件也可以正常使用,唯一的问题是管理面板。
    • 出于安全原因,您不应将文件的所有者设置为运行服务器的实际用户。
    猜你喜欢
    • 2022-10-06
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多