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

Django app on Azure not getting static files

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

我建议您查看 Whitenoise 以使用 Django 为您的静态文件提供服务。自从集成 Whitenoise 以来,我在为他们服务时没有遇到任何问题。尝试将其替换为您当前的静态文件查找器设置。

http://whitenoise.evans.io/en/stable/django.html

注意几点:

下一行会改变

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

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

You are using pointing to completely different folder here. Hence always empty You need the collectstatic command to copy files to Project > static directory


下面删除行os.path.join(BASE_DIR, 'static/')...

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'), #Not Required since you already mentioned it in STATIC_URL
('myapp', os.path.join(BASE_DIR, 'myapp', 'static')),
)

原因:

  • STATIC_ROOT为静态文件存放后的文件夹 使用 manage.py collectstatic

The absolute path to the directory where collectstatic will collect static files for deployment.

If the staticfiles contrib app is enabled (default) the collectstatic management command will collect static files into this directory. See the howto on managing static files for more details about usage.

  • STATICFILES_DIRS 是 Django 将搜索的文件夹列表 除了每个应用程序的静态文件夹之外的其他静态文件 安装。

This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.

如果问题仍然存在,请检查您在 apache 中的静态文件目录 mod_wsgi 服务器中的配置文件是否配置正确。

检查此 link 以获得相关帮助 >> https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/modwsgi/


干杯!!!

找到了!!

只需添加:+ 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)

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

Django 在生产模式下不提供静态文件。

它唯一一次为静态文件提供服务是当您在开发模式下设置 DEBUG = True 时,不建议在生产设置中这样做。

Django 建议通过 CND 或任何其他网络服务器提供静态文件。但是,如果您的网站很小并且流量不高,您可以使用 Django whitenoise 提供静态文件,下面是您的做法。

以下解决方案适用于 python 3.7 和 Django 3.2

第一步:安装whitenoise包

pip install whitenoise

第 2 步:确保您的 BASE_DIR 如下所示

BASE_DIR = Path(__file__).resolve().parent.parent

第 3 步:将这些添加到您的 settings.py。如果您收到任何错误,请尝试注释掉 STATICFILES_STORAGE 并检查

STATIC_ROOT = BASE_DIR / 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

第 4 步:WhiteNoise 中间件应直接放在 Django SecurityMiddleware(如果您正在使用它)之后和所有其他中间件之前

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

第 5 步:确保这是您在模板中引用静态文件的方式(还要检查您的模板中是否提到了 {% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'appname/styles.css' %}">

第 6 步:运行 收集静态数据

python manage.py collectstatic

第 7 步:将 DEBUG = False 并 运行 服务器验证其是否正常工作。

需要进一步阅读的一些额外资源:

whitenoise
Django on Azure - beyond "hello world"