1

I cant figure out how to make Azure App Service use css for admin panel. In settings.py I included these:

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

But somehow css is still not working for the admin panel. How can I fix this? As far as I know Oryx which works under Azure App Service should automatically run

python manage.py collectstatic

Any ideas?

EDIT: Installed apps

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',
]
Alex T
  • 3,529
  • 12
  • 56
  • 105
  • make sure that you have 'django.contrib.staticfiles' in installed_apps, is this an apache server? – Sumithran Mar 11 '21 at 16:43
  • @Sumithran I have it. Im not sure if this is Apache server, its basic azure app service app. – Alex T Mar 12 '21 at 08:07
  • Hi @AlexT, how are things going? I noticed that `@DorisLv` has shared some suggestions in the answer. Please have a check with it. If it helpful to you, you can mark it as the solution. Thanks. – Bright Ran-MSFT Mar 17 '21 at 08:02

2 Answers2

1

There are something to check might help:

  1. Click F12 on your Admin page to check if it is the 404 Not Found error.

  2. Check the static files exist in your azure web app or not. You could see it in https://{your-web-app}.scm.azurewebsites.net/wwwroot/ site.

  3. Check if your file structure match with the settings.py. Any spell error would make the static file not found.

  4. If your file exists well, you should check if you use them correctly in your code like this answer. Here is how he use:

     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)
    
Doris Lv
  • 3,083
  • 1
  • 5
  • 14
1

Firstly try to add these code lines to settings.py

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

Then import these lines to the 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

Finaly you have to add these lines to urlpatterns in urls.py

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

(If you have not already created a static file please make sure to create one before you try this)
It's worked severl times for me and hope this will work for you too

zpvk
  • 124
  • 1
  • 10