apache Django css和javascript应用程序未在生产中提供

toiithl6  于 2023-03-09  发布在  Apache
关注(0)|答案(2)|浏览(160)

我有一个django项目,在开发模式下css加载得很好,但在生产中没有。
在我的www.example.com文件中,我设置了以下三个变量settings.py file I've set these trhee variables

STATIC_ROOT = BASE_DIR / 'staticfiles/'
STATIC_URL = 'static/'
STATICFILES_DIRS = [BASE_DIR / 'static',]

我还设置了DEBUG = False
当我运行python www.example.com collectstatic命令时,staticfiles文件夹正确接收文件。我在项目的根目录下创建了一个静态文件夹以安装 Bootstrap ,同样,在developent中运行良好。我通过XAMPP使用Apache服务器,因为我在Windows上。项目的配置如下:manage.py collectstatic command the staticfiles folder receives the files properly. I created a static folder in the root of the project to install bootstrap there, again, in developent runs just fine. I'm using Apache server throug XAMPP becasuse I'm on Windows. the configuration for the project is next:

LoadFile "C:/Python310/python310.dll"
LoadModule wsgi_module "C:/Users/life/.virtualenvs/outgoing-qJCH8A9k/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Users/life/.virtualenvs/outgoing-qJCH8A9k"

WSGIScriptAlias /outgoing "D:/DEV/PYTHON/GASTOS/software/outgoing/project/wsgi.py" application-group=%{GLOBAL}
WSGIPythonPath "D:/DEV/PYTHON/GASTOS/software/outgoing"

<Directory "D:/DEV/PYTHON/GASTOS/software/outgoing/project">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static/ "D:/DEV/PYTHON/GASTOS/software/outgoing/staticfiles/"

<Directory "D:/DEV/PYTHON/GASTOS/software/outgoing/staticfiles">
Require all granted
</Directory>`

重点是,应用程序加载良好,但没有引导CSS和JavaScript文件。浏览器控制台也告诉这一点。

Refused to apply style from 'http://localhost/outgoing/static/node_modules/bootstrap/dist/css/bootstrap.min.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

GET http://localhost/outgoing/static/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js

请帮帮忙。

ffscu2ro

ffscu2ro1#

如果你在生产环境中,建议使用Django的WhiteNoise库,这是直接在生产环境中提供静态文件/资产的最简单方法之一。

pip install whitenoise

在您的www.example.com中settings.py

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', 
......,
]
# 
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'
#

或者,您可以在提供静态文件时减小它们的大小(这样更有效)。settings.py:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

根据生产服务器,您可以通过以下方式收集静态数据:

python manage.py collectstatic
q9yhzks0

q9yhzks02#

使用DEBUG=False时,预计static files文件夹将位于外部资源上,因为这会给站点带来漏洞。

相关问题