Azure 上的 Django 应用程序未获取静态文件

标签 django azure azure-web-app-service django-staticfiles

在 Azure Web 应用程序上获取我的 Django 项目,但是当我在 SSH 终端上调用时:

Python manage.py collectstatic

它说复制了 252 个文件,但我的静态文件在我的模板上不可见,并且 wwwroot 中的静态文件夹是空的......这是我的 wwwroot 结构:

wwwroot
|---Myproject
|---manage.py
|---oryx-manifest.toml
|---hostingstart.html
|---static //With all my static files
├── myapp
│   ├── migrations
│   ├── __pycache__
│   ├── static
|   |   |---Images
|   |   |   |--myimage.png
|   |   |   |--myimage2.png
│   └── templates

这是我的设置.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
)

知道为什么或者我做错了什么吗?Azure 收集的数据是否不同?

编辑>当我访问我的网站时,我的图像不显示...我在模板上这样调用它们:

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

编辑2/////

在 wwwroot 中确实创建了一个包含所有静态数据的文件夹,但是当我加载模板时它们没有显示,在 wen 控制台中我收到 myimage.png 和 myimage2.png 的错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

最佳答案

找到了!!

只需添加以下内容: + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 到如下 URL 模式:

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
        path('myapp/', include('myapp.urls')),
        path('admin/', admin.site.urls),
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

它成功了,希望对其他人有帮助!!

关于Azure 上的 Django 应用程序未获取静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65431326/

相关文章:

azure - AzCopy - 在带有 $(美元符号)的位置中断

c# - 使用 key 保管库中的证书创建 Azure Web 应用 SSL 绑定(bind)

azure - 我不断在我的 azure 站点 log tail 命令中收到此消息

python - 多个模型上的 Django GenericForeignKey 字段

python - HttpResponse Django 不更改文件名

python - Django有序序列模型设计

mysql - ASP.NET Azure MySQL 代码优先迁移 : Table 'xxx' already exists

azure - 使用连接字符串访问 Azure blob 存储和 AD B2C Web 应用程序中的资源是否安全

Django makemigrations : ValueError: Lookup failed for model referenced by field

javascript - 我可以以编程方式从 Power BI Embedded 报表导出筛选的数据吗?