简体   繁体   English

为什么 django 管理模板不加载

[英]why django admin template doesn't load

Since I had started Django, I'm tackling some ridiculous problems.自从我开始 Django 以来,我正在解决一些荒谬的问题。 Recently, when I start a new project and I run it on the server, Django's admin CSS is not loaded.最近,当我开始一个新项目并在服务器上运行它时,没有加载 Django 的管理员 CSS。 The last project I ran on the server, after a while it was okay, and the real Django admin template was there and the CSS was loaded and it was working.我在服务器上运行的最后一个项目,过了一会儿就好了,真正的 Django 管理模板在那里,CSS 已加载并且正在工作。 But this time again the same problem happened and I don't know how to solve it cause the project here is the photo is raw, with no special code.但是这一次又发生了同样的问题,我不知道如何解决它,因为这里的项目是照片是原始的,没有特殊的代码。 I don't know if it's Chrome problem or not, but I have tried it on other browsers and it was the same thing.我不知道是不是 Chrome 的问题,但我在其他浏览器上尝试过,结果是一样的。

I would be glad if you can help me如果你能帮助我,我会很高兴

If this problem is happening locally, then just change your settings.py如果这个问题发生在本地,那么只需更改您的 settings.py

DEBUG = True
#...
#...
STATIC_ROOT = YOUR_STATIC_ROOT_DIRECTORY

Then run collectstatic, make sure you have a proper STATIC_ROOT directory which is also in your settings.py file然后运行 collectstatic,确保您有一个正确的STATIC_ROOT目录,该目录也在您的settings.py文件中

python manage.py collectstatic

If you have deployed your app on a production server then, you have to follow certain things in order to get stylesheet and javascript files.如果您已经在生产服务器上部署了您的应用程序,那么您必须遵循某些事情才能获取样式表和 javascript 文件。

Firstly In your urls.py you need to add this code首先在您的urls.py中,您需要添加此代码

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
               static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Then you have to use a third-party media file server(CDN) Like AWS S3 or you can serve your css, js or media files from your Django server If you don't want to use AWS S3, then you can either send css/js using your django app (which is not ideal to do), or you can use nginx or apachee to send your css/js然后您必须使用第三方媒体文件服务器(CDN),例如 AWS S3,或者您可以从 Django 服务器提供 css、js 或媒体文件如果您不想使用 AWS S3,那么您可以发送 css /js 使用您的 django 应用程序(这并不理想),或者您可以使用 nginx 或 apachee 发送您的 css/js

For Nginx to send js/css You have to add a Static and Media File Root that can be accessed by your django application, I usually use /var/www/ to serve static and media from any Linux server For Nginx to send js/css You have to add a Static and Media File Root that can be accessed by your django application, I usually use /var/www/ to serve static and media from any Linux server

STATIC_ROOT = '/var/www/static'
MEDIA_ROOT = 'var/www/media'

Then if you are using nginx那么如果你使用的是 nginx

server {
    server_name domainname.com;

    access_log off;

    location /static/ {
        root /var/www/static;
    }
    location /media/ {
        root /var/www/media;
    }

If it is still not working, then your django app might not be able to use the given static_root and media directory, make sure your app has access to them如果它仍然不起作用,那么您的 django 应用程序可能无法使用给定的 static_root 和媒体目录,请确保您的应用程序可以访问它们

If you want to send your js/css from your django app (Better not to do in production) Then如果您想从您的 django 应用程序发送您的 js/css(最好不要在生产中执行)然后

To install whitenoise安装whitenoise

pip install whitenoise

In your settings file在您的设置文件中

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

You must set some configuration of Django deployment in this link: Django Deployment Check list https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/您必须在此链接中设置 Django 部署的一些配置:Django 部署检查清单Z5E056C500A1C4B6A7110B50D807BADEcheck5Z://docs.djangodloyproject.com/en/4.0/howto

the answer I found was to change STATIC_URL = 'static/' in settings.py to STATIC_URL = '/static/' .我找到的答案是将 settings.py 中STATIC_URL = 'static/'更改为STATIC_URL = '/static/' Only one '/' may change your whole admin appearance.只有一个“/”可能会改变您的整个管理员外观。 This problem may not happens to everyone but running code in Pycharm, I had been tackling it for such a long time.这个问题可能不会发生在每个人身上,但在 Pycharm 中运行代码,我已经解决了这么长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM