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

在 Azure webapp 上获得了我的 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

这是我的settings.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)

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

  • 不要忘记导入设置:`from django.conf import settings`

以上是Azure上的Django应用程序没有获取静态文件的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>