首页 > 解决方案 > Azure 上的 Django 应用程序从静态文件中获取图像,而不是从 Bootstrap、Jquery 或 CSS 文件中获取图像

问题描述

在 Azure 上部署了一个具有以下结构的 Django 应用程序:

wwwroot
|---Myproject
|---manage.py
|---oryx-manifest.toml
|---hostingstart.html
|---static //Same static folder and same files from myapp 
├── myapp
│   ├── migrations
│   ├── __pycache__
│   ├── static
│   │   │---bootstrap-3.3.7-dist
│   │   │   │--css
│   │   │   │  │--bootstrap.min.css
│   │   │   │  js
│   │   │   │  │--bootstrap.min.js
│   │   │---Datatables
│   │   │   │--jQuery-3.3.1
│   │   │   │  │--jquery-3.3.1.js
│   │   │   │  │datatables.js
│   │   │   │  │datatables.css
|   |   |---Images
|   |   |   |--myimage.png
|   |   |   |--myimage2.png
│   └── templates

问题是,当我以这种方式在模板上调用静态时:

<script src="{% static 'Datatables/jQuery-3.3.1/jquery-3.3.1.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
<script type="text/javascript" charset="utf8" src="{% static 'Datatables/datatables.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'Datatables/datatables.css' %}">

<img src="{% static 'Images/myimage.png' %}" align="center"/><br>

图像可以完美呈现,但 CDN 数据表和 css 样式不能,并且浏览器控制台会向我抛出此错误:

bootstrap.min.js:6 Uncaught Error: Bootstrap's JavaScript requires jQuery
    at bootstrap.min.js:6

和 :

myappwapp.azurewebsites.net/:1 Refused to apply style from 'https://myapp.azurewebsites.net/static/Datatables/datatables.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

没有意义,在模板上调用脚本的顺序没问题(已经尝试过不同的顺序),此外,图像从那里的同一个静态文件夹加载得很好,如果我在本地机器上运行它,一切都很好......知道怎么了?

提前致谢。

编辑 ////// :settings.py

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

STATIC_URL = '/static/'
STATICFILES_DIRS = (
   ('myapp', os.path.join(BASE_DIR, 'myapp', 'static')),
)

STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

标签: djangoazure-web-app-servicecdndjango-staticfilesazure-static-web-app

解决方案


将以下代码添加到settings.py.

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]

原因

您的文件夹似乎static处于错误的级别。

相关文章

1. Django 模板中不包含 Bootstrap 静态文件

2. BOOTSTRAP // 尽管添加了 cdn 链接和参考样式链接,但仍无法正常工作 // PYTHON


推荐阅读