簡體   English   中英

Azure 應用服務托管 Django APP 中的管理面板中沒有 CSS

[英]No CSS in Admin panel in Azure App Service hosted Django APP

我不知道如何讓 Azure App Service 使用 css 作為管理面板。 在 settings.py 我包括這些:

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

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

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

但是不知何故 css 仍然不能用於管理面板。 我怎樣才能解決這個問題? 據我所知,在 Azure App Service 下工作的 Oryx 應該會自動運行

python manage.py collectstatic

有任何想法嗎?

編輯:已安裝的應用程序

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'licenses_api',
]

有一些東西需要檢查可能會有所幫助:

  1. 在您的管理頁面上單擊 F12 以檢查它是否是404 Not Found錯誤。

  2. 檢查 azure web 應用程序中是否存在 static 文件。 您可以在https://{your-web-app}.scm.azurewebsites.net/wwwroot/站點中看到它。

  3. 檢查您的文件結構是否與settings.py匹配。 任何拼寫錯誤都會導致找不到 static 文件。

  4. 如果您的文件存在良好,您應該檢查您是否在代碼中正確使用它們,例如這個答案 他是這樣使用的:

     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)

首先嘗試將這些代碼行添加到settings.py

STATIC_URL = '/static/'
if DEBUG:
  STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
else:
  STATIC_ROOT = os.path.join(BASE_DIR, 'static')

然后將這些行導入urls.py

from django.conf.urls.static import static
from django.conf import settings
from django.conf.urls import url
from django.views.static import serve

最后,您必須將這些行添加到urls.py中的 urlpatterns

url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

(如果您還沒有創建static文件,請確保在嘗試之前創建一個)
它對我有用很多次,希望這對你也有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM