为什么django的管理面板在生产版本中出现不同?

时间:2017-12-27 13:46:53

标签: python django django-admin

首先,我不知道这只是我的问题还是别人也有。我的django admin的生产版本(我在我的网站上使用它)看起来与我看到的不同,当我在我的个人电脑上吃午餐时。

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

local version(127.0.0.1:8000)

production version(mywebsite)

我想提一下:

我的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之后我还需要做些什么吗?

2 个答案:

答案 0 :(得分:1)

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

同时更改

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

有关

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

答案 1 :(得分:-1)

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

sudo chown -R user:www-data static

用户=您当前的系统用户。