简体   繁体   English

django:未找到运行服务器 static 文件

[英]django: runserver static files not found

I've read some posts on similar topics but none of them are exactly the same.我读过一些关于类似主题的帖子,但没有一个是完全相同的。

I've a django app (django 2.2.2, python 3.6.12) that works with no problems when served with uwsgi (along with nginx), but has problems of finding static files when served with runserver (python manage.py runserver 0:8000) with error messages like the following: I've a django app (django 2.2.2, python 3.6.12) that works with no problems when served with uwsgi (along with nginx), but has problems of finding static files when served with runserver (python manage.py runserver 0 :8000) 错误消息如下:

WARNING Not Found: /static/admin/css/base.css
WARNING Not Found: /static/stylesheets/css/style.css
WARNING Not Found: /static/admin/js/core.js

These WARNINGs are caused by lines in the accessed webpage like the following.这些警告是由访问网页中的行引起的,如下所示。

<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">
<script type="text/javascript" src="/static/admin/js/core.js"></script>

Of course the webpage is not displayed correctly due to not being able to access the static files.当然,由于无法访问 static 文件,网页无法正确显示。

I have the following settings:我有以下设置:

BASE_PATH = '/data/webapps/myapp'
STATIC_ROOT = os.path.join(BASE_PATH, '_site/static')

and in BASE_PATH, I have folders like the following which contain all files the system complained about not being found:在 BASE_PATH 中,我有如下文件夹,其中包含系统抱怨找不到的所有文件:

_site/static/admin/css
_site/static/admin/js
_site/static/stylesheets/css

I even duplicated all folders under _site directly under BASE_PATH like the following, but with no effect.我什至直接在 BASE_PATH 下复制了 _site 下的所有文件夹,如下所示,但没有任何效果。

static/admin/css
static/admin/js
static/stylesheets/css

Here I want to emphasize that it works with no problems when served with uwsgi with the following command:在这里我想强调的是,当使用以下命令与 uwsgi 一起使用时,它可以正常工作:

uwsgi --ini uwsgi.ini

and uwsgi.ini contains the following lines:并且 uwsgi.ini 包含以下几行:

[uwsgi]
chdir          = %d
enable-threads = true
max-requests   = 5000
file           = /data/webapps/myapp/myapp/conf/wsgi.py
socket         = uwsgi.sock
chmod          = 666
chmod-socket   = 666
uid            = myapp
gid            = myapp

and wsgi.py has the following codes:和 wsgi.py 有以下代码:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mybic.conf.settings')
application = get_wsgi_application()

Any help are appreciated.任何帮助表示赞赏。

PS附言

The findstatic command can't find those files either. findstatic 命令也找不到这些文件。 For example, the following command found no matching.例如,以下命令找不到匹配项。

python manage.py findstatic --verbosity=2 core.js

Adding a vector STATICFILES_DIRS with all folder paths for the static files, such as the following, would only help the findstatic command but not the runserver command, that is, there're still the WARNINGs from accessing the webpage.添加带有 static 文件的所有文件夹路径的向量 STATICFILES_DIRS ,如下所示,只会帮助 findstatic 命令而不是 runserver 命令,也就是说,仍然存在访问网页的警告。

STATICFILES_DIRS = [
    os.path.join(BASE_PATH, '_site/static/stylesheets/css'),
    os.path.join(BASE_PATH, '_site/static/stylesheets/js'),
    os.path.join(BASE_PATH, '_site/static/admin/css'),
    os.path.join(BASE_PATH, '_site/static/admin/js'),
    ...
]

As @NguyễnVũThiên pointed out, it has something to do with debug mode.正如@NguyễnVũThiên 指出的那样,它与调试模式有关。 When debug is turned off (either set DEBUG to False or not setting DEBUG at all), runserver will no longer serve static files.关闭调试时(将 DEBUG 设置为 False 或根本不设置 DEBUG),runserver 将不再提供 static 文件。 Unfortunately the WARNINGs are quite misleading.不幸的是,警告非常具有误导性。 Instead of saying Not found, they should say Not served (and say something about debug mode).他们应该说未服务,而不是说未找到(并说一些有关调试模式的内容)。

So to resolve the problems, as suggested in the link @NguyễnVũThiên gave, either turn on the debug mode (DEBUG = True) or add the --insecure option to runserver (python manage.py runserver --insecure).因此,要解决问题,正如@NguyễnVũThiên 给出的链接中所建议的那样,打开调试模式(DEBUG = True)或将 --insecure 选项添加到 runserver(python manage.py runserver --insecure)。

Change DEBUG = TRUE .更改DEBUG = TRUE If it's work just following this post to resolve the problems.如果它只是按照这篇文章解决问题的工作。

Keep DEBUG = False , because many security reasons.保持DEBUG = False ,因为许多安全原因。

Install pip install whitenoise (current is ver 5.2.0)安装pip install whitenoise (当前版本为 5.2.0)

In your settings.py :在您的settings.py

  1. Add 'whitenoise.middleware.WhiteNoiseMiddleware', into MIDDLEWARE'whitenoise.middleware.WhiteNoiseMiddleware',添加到MIDDLEWARE
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # Add WhiteNoise here
    ...
]
  1. Add STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage' bellow your STATIC_ROOT在您的STATIC_ROOT下方添加STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
if DEBUG:
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static')
    ]
    ...

else:
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

  1. In your folder which contain manage.py run python manage.py collectstatic (For static file of Django Admin site)在包含manage.py的文件夹中运行python manage.py collectstatic (对于 Django 管理站点的 static 文件)

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

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