0

I am new to python & django. I have learnt basic of both and made a simple project called PythonDjangoDemo. I have visited admin urls during development. But I am unable to load css for admin url in python-django project. At first lets look at project structure. I have -

PythonDjangoDemo
  |--- PythonDjangoDemo
  |      |--- __init__.py
  |      |--- settings.py
  |      |--- urls.py
  |      |--- wsgi.py
  |--- static_cdn
  |      |--- admin
  |      |      |--- css
  |      |      |      |---base.css
  |      |      |      |---login.css
  |      |      |--- fonts
  |      |      |--- img
  |      |      |--- js
  |--- media_cdn
  |--- templates
  |--- manage.py
  |--- db.sqlite3

Let's have a look at settings.py

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

... ... ...

STATIC_URL = '/static/'
MEDIA_URL = "/media/"

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media_cdn")

I have put all my css for admin urls at "static_cdn/admin/css". When I try to access "127.0.0.1:800/admin" I do not get any error(s). Here is the console output -

[30/Apr/2017 16:04:58] "GET /admin/ HTTP/1.1" 302 0
[30/Apr/2017 16:04:59] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1650
[30/Apr/2017 16:04:59] "GET /static/admin/css/base.css HTTP/1.1" 200 16066
[30/Apr/2017 16:04:59] "GET /static/admin/css/login.css HTTP/1.1" 200 1203
Not Found: /favicon.ico
[30/Apr/2017 16:04:59] "GET /favicon.ico HTTP/1.1" 404 2329

Could you please explain

  • What is going on wrong compared to the console response (that is no error)?
  • And what is the meaning of STATIC_URL and STATIC_ROOT?
rrawat
  • 1,071
  • 1
  • 15
  • 29
testuser
  • 793
  • 3
  • 13
  • 35
  • This is with ``DEBUG = True`` or ``False``? – Grimmy Apr 30 '17 at 13:26
  • Also did you run ``collectstatic``? – Grimmy Apr 30 '17 at 13:30
  • `DEBUG = True` and I also run `collectstatic` @Grimmy – testuser Apr 30 '17 at 13:41
  • @Grimmy , take a look -- I put css files at `/static_cdn/admin/css/base.css`. But console output is -- `/static/admin/css/base.css`. Is it `static` or `static_cdn`? – testuser Apr 30 '17 at 13:58
  • 1
    @Grimmy , There is no error detected in the browsers [checked in chrome & firefox] also. But they are showing --- `Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "http://localhost:8000/static/admin/css/base.css".` **and** `Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "http://localhost:8000/static/admin/css/login.css".` – testuser Apr 30 '17 at 14:20

4 Answers4

2

Yes I got the solution to the problem. Though everything is OK but the browser's console shows -

Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "http://localhost:8000/static/admin/css/base.css". 
Resource interpreted as Stylesheet but transferred with MIME type application/x-css: "http://localhost:8000/static/admin/css/login.css".

The problem was discussed at python built in server not loading css.

I got the solution at CSS not loading wrong MIME type Django.

Community
  • 1
  • 1
testuser
  • 793
  • 3
  • 13
  • 35
0

your css is loading fine as you can see there is no error.

check this for details.

Also your media folder should be at the same level as the static folder.

rrawat
  • 1,071
  • 1
  • 15
  • 29
0

you should put static_cdn folder in parallel to media_cdn folder.

Then clear the cache and load admin page.

Rahul Gupta
  • 504
  • 4
  • 17
0
STATIC_URL = '/static/'

STATIC_ROOT =  os.path.join(BASE_DIR, "static", "static")

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static", "static-only"),

I put my static files as above and created it in my Django base directory. Afterwards I ran django manage.py collectstatic. Then ran django manage.py runserver but it still didn't work. Until I cleared my BROWSER CACHE.

YakovL
  • 7,557
  • 12
  • 62
  • 102