[英]Django - CSS File Not Loading In Production (Debug: False)
Current error (Debug=False) 当前错误(Debug = False)
"[16/Jan/2018 15:18:42] "GET /static/style.css HTTP/1.1" 404 90" “ [16 / Jan / 2018 15:18:42]” GET /static/style.css HTTP / 1.1“ 404 90”
Web site loads but broken formatting because CSS file not loaded 网站已加载,但格式未正确,因为未加载CSS文件
Logging: 记录:
On the CMD prompt it says 在CMD提示下说
"[16/Jan/2018 15:49:05] "GET /beginners/ HTTP/1.1" 200 3760
[16/Jan/2018 15:49:05] "GET /static/style.css HTTP/1.1" 404 90
"
I'm not sure why this isn't working: my style.css is located in my static folder, and the static folder is the same folder as manage.py 我不确定为什么这行不通:我的style.css位于我的静态文件夹中,并且该静态文件夹与manage.py处于同一文件夹
When I set Debug = True, I reload the page and it works fine - my static folder is active and I get no static error: 当我设置Debug = True时,我重新加载了页面,并且工作正常-我的静态文件夹处于活动状态,并且没有任何静态错误:
[16/Jan/2018 15:58:11] "GET /beginners/? HTTP/1.1" 200 3759
[16/Jan/2018 15:58:11] "GET /static/style.css HTTP/1.1" 200 5014
Please help!! 请帮忙!!
STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
You are using Django's development server with Debug=False. 您正在使用Debug = False的Django开发服务器。 Django will not serve static content when Debug is False.
当Debug为False时,Django将不会提供静态内容。
Django development server is not intended to be used in production. Django开发服务器不适用于生产环境。
You will need a Web Server which will serve your static content (or put it on a CDN) 您将需要一个Web服务器来提供您的静态内容(或将其放在CDN上)
Common deployment styles used with Django are Django常用的部署样式是
nginx -> uwsgi -> django nginx-> uwsgi-> django
apache -> mod_wsgi -> django apache-> mod_wsgi-> django
There's also gunicorn which is relatively easier to set up. 也有相对容易设置的gunicorn。
The above answer is not entirely true... There's another way! 上面的答案并不完全正确...还有另一种方法! Took me forever to figure out :(
让我永远找出来:(
Step 1: If you are/were using them, make sure that all traces of WhiteNoise and django_heroku are removed: from imports, INSTALLED_APPS, virtual environment, etc. 第1步:如果您在/使用它们,确保白噪声和django_heroku的所有痕迹被删除:来自进口,INSTALLED_APPS,虚拟环境等。
Step 2: 第2步:
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "path/to/other/static/dir"),
]
The key for me was using the proper storage engine which is set by STATICFILES_STORAGE. 对我来说,关键是使用由STATICFILES_STORAGE设置的正确存储引擎。 You don't need to set STATIC_ROOT because the root will be generated for you by the engine.
您无需设置STATIC_ROOT,因为引擎将为您生成根目录。 More on that in the warning section here: https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-STATIC_ROOT
有关更多信息,请参见此处的警告部分: https : //docs.djangoproject.com/en/1.11/ref/settings/#std : setting-STATIC_ROOT
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
Then finally 然后最后
$ python manage.py collectstatic
$ python manage.py runserver
If this doesn't help, don't forget logging: With DEBUG=False, how can I log django exceptions to a log file 如果这样做没有帮助,请不要忘记记录日志: 使用DEBUG = False,如何将Django异常记录到日志文件中
This kind of helped: https://docs.djangoproject.com/en/1.11/howto/static-files/ 这种帮助: https : //docs.djangoproject.com/en/1.11/howto/static-files/
By default, when DEBUG = False in production, django will not serve static files. 默认情况下,当生产中的DEBUG = False时,django将不提供静态文件。
After deploying your application you'll need to run python manage.py collectstatic
to put all your static files into STATIC_ROOT. 部署应用程序后,您需要运行
python manage.py collectstatic
将所有静态文件放入STATIC_ROOT中。
I introduce Whitenoise which allows your web app to serve its own static files. 我介绍了Whitenoise,它允许您的Web应用程序提供其自己的静态文件。 It works with any WSGI-compatible app.
它可以与任何WSGI兼容的应用程序一起使用。
To install it run: 要安装它,请运行:
pip install whitenoise
Edit your settings.py file and add WhiteNoise to the MIDDLEWARE_CLASSES list, above all other middleware apart from Django's SecurityMiddleware: 编辑您的settings.py文件,并将WhiteNoise添加到MIDDLEWARE_CLASSES列表中,除Django的SecurityMiddleware之外,在所有其他中间件之上:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
# ...
]
Save and restart your server 保存并重新启动服务器
Finaly if you want gzip functionality, add this to your settings.py 最后,如果需要gzip功能,请将其添加到settings.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
save and restart 保存并重启
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.