【问题标题】:Django app on Azure getting images from static files but not Bootstrap, Jquery or CSS filesAzure 上的 Django 应用程序从静态文件中获取图像,而不是从 Bootstrap、Jquery 或 CSS 文件中获取图像
【发布时间】:2021-09-09 01:43:21
【问题描述】:

在 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',
)

【问题讨论】:

  • 试试我的答案,如果不适合你,请告诉我。

标签: django azure-web-app-service cdn django-staticfiles azure-static-web-app


【解决方案1】:

将以下代码添加到settings.py

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

原因

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

相关帖子

1. Bootstrap static files not being included in Django template

2. BOOTSTRAP // not working despite adding cdn links and reference style link // PYTHON

【讨论】:

  • 感谢您的友好回复,我已经尝试过您的代码,但得到了这个:错误:?:(staticfiles.E002)STATICFILES_DIRS 设置不应包含 STATIC_ROOT 设置.....我已经用我如何设置静态数据更新了我的问题。
猜你喜欢
  • 2018-10-20
  • 1970-01-01
  • 2019-04-16
  • 2021-04-02
  • 2012-05-27
  • 1970-01-01
  • 2016-10-11
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多