148

I made a Django admin site using Django development version but it isn't being styled:

alt text

5
  • 2
    Is there a style referenced that does not get loaded? Commented Dec 12, 2010 at 4:24
  • 1
    @Pekka ,yes, it is the django default style , it is in D:\Python25\Lib\site-packages\django\contrib\admin\media Commented Dec 12, 2010 at 4:57
  • 1
    I had the same issue of admin static content not being vieable, but with nginx (instead of development environment). Basically, check your nginx config file and make sure it's the location of your 'collectstatic'. Details here: serverfault.com/questions/403264/… Commented Jun 4, 2014 at 14:50
  • 1
    Check settings.py whether the DEBUG = True is set or not. If not set it Commented Nov 11, 2023 at 4:19
  • 1
    Updated URL for Adam's comment: docs.djangoproject.com/en/5.1/howto/static-files Commented Aug 16, 2024 at 13:56

25 Answers 25

107

After setting up your STATIC_ROOT and STATIC_URL, you may have to run

python manage.py collectstatic 
Sign up to request clarification or add additional context in comments.

4 Comments

This works on 1.5.2 also. I didn't have to change or add any file or code
Maybe useful for somebody: you should use the same name for STATIC_ROOT and STATIC_URL, example: I combined "staticfiles" and "static", used STATIC_ROOT=os.path.join(BASE_DIR, "staticfiles")" and STATIC_URL="/static/". My app worked fine, but the admin files could not be found. so change it both to "staticfiles" or to "static"
Tried multiple things, but this resolved the problem.
culprit was STATIC_ROOT settings, after updating UI components rendered properly
64

I broke my head over it for two days trying whatnot!

Finally, changed DEBUG in the settings.py file to:

DEBUG = True 

and it worked.

P.S:

SECURITY WARNING: don't run with debug turned on in production! 

1 Comment

That's all it was for me.
30

ADMIN_MEDIA_PREFIX is deprecated now, use STATIC_URL instead. Setting STATIC_URL = '/static/' in settings.py should do the job. Try:

import os.path PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__)) 

and then:

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

Works on Django 1.4 pre-alpha SVN-16920.

3 Comments

This doesn't work for me also. I notice that if I move (manually) the folder static/admin inside static/css/ then I can access the css files. So it seems that I can access the css files just if the are inside the static/files but the collectstatic doesn't put them there. Of course, moving the folder manually just doesn't work because the templates continue pointing to static/admin.
Worked for me when trying to run the admin interface created with Django v1.2, tested on 1.3, and then re-deployed on v1.4. Running Ubuntu 12.04 LTE
In my case, I had to move the static folder inside the app, just make sure that PROJECT_ROOT is pointing to the root to the setting.py. In my case it was pointing to the parent folder.
18

Django does not serve static files on it's own. You have to tell it where the files are.

The ADMIN_MEDIA_PREFIX in the settings.py will point Django in the right location.

Since you're using the development version, you'll want the dev-specific document for static files how-to. Adam's link will lead you to the 1.2 version.

2 Comments

Good answer but now deprecated and absent from settings.py. Read below for more up-to-date solutions.
I have same error but in Flask, I put my css inside static folder. And use jinj2 2 suntax i.e {{url_for('static', filename='css/style.css')}} , but still on my laptop I have this error. I forced refresh the page but problem still exist. Please help
10

I read several other threads trying to fix this...resorted to an alias as in other threads. This assumes that your own custom app is serving static files correctly, which would indicate that your STATIC_ROOT and STATIC_URL have proper settings.

STATIC_ROOT = '' STATIC_URL = '/static/' 

Then (from your static directory):

ubuntu@ip-1-2-3-4:/srv/www/mysite.com/app_folder/static$ sudo ln -s /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/ admin 

Hope this helps someone...there are a lot of threads on this topic.

Comments

9

I ran into this issue as well following the Django Book Tutorial. In Chapter 5|Installing the model, the book states when referring to the default INSTALLED_APPS- "Temporarily comment out all six of those strings by putting a hash character (#) in front of them." http://www.djangobook.com/en/2.0/chapter05.html

Then, in Chapter 6, the Book tells the reader to uncomment 4 of those 6 lines- "note that we commented out these four INSTALLED_APPS entries in Chapter 5. Uncomment them now."

But the statcifiles line is what is needed to restore CSS to the admin page, so uncomment that 'django.contrib.staticfiles',

2 Comments

Uncommenting 'django.contrib.staticfiles' in settings.py file as suggested above worked for me too. 'django.contrib.staticfiles' should be present by default in your settings.py file under INSTALLED_APPS. Thanks Chris for your suggestion.
Got here from the Django Book too. That one-character removal does it.
7

I see there are many answers but none of them worked for me, so I'm posting my own. What solved it for me was adding a static files URL to the root URLs of the app. I needed to add this URL to my URLs list:

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

You will also need these two imports:

from django.conf import settings from django.conf.urls.static import stati 

More can be viewed in this article.

Comments

6

In /project_name/project_name/settings.py you need to set STATIC_URL to tell your site what url to use for static files.

Then set STATIC_ROOT to be some folder on your filesystem that is not the same as any of your directories listed in STATICFILES_DIRS list.

Once STATICFILES_ROOT is set, you would run python manage.py collectstatic from the project directory.

This will copy all the admin static files and all files in any other folders listed in the STATICFILES_DIRS list. Basically this puts all your static files in one place so you you can move them to your CDN when deploying your site. If you are like me and don't have a CDN, then you have two options:

  1. Add the folder you set as STATIC_ROOT to the STATICFILES_DIRS list. This will allow the staticfiles finders in django to locate all the static files.
  2. Move the entire folder of static files somewhere else on your file system and direct STATICFILES_DIRS to include that new location.

I make no comments about security with this answer, it is just the way I have been able to develop with my web server for small projects. I expect that you will want a CDN as django suggest if you are doing anything larger scale.

UPDATE: I just ran into this issue and this method didn't quite do what I think you want. What ended up working for me was after I ran collectstatic I just copied the admin static files that it put into STATICFILES_ROOT into the directory that I had used for my own static files. That solved the issue for me.

Comments

4

run: python manage.py collectstatic

Add this line to Vhost which located at : /etc/apache2/sites-available/000-default.conf

Alias /static/admin/ /var/www/html/example.com/static/admin

Here is entire Vhost setting for django setup

<VirtualHost *:80> ServerName gautam.tech ServerAlias www.gautam.tech WSGIDaemonProcess gautam.tech python-path=/var/www/html/gautam.tech python-home=/var/www/html/gautam.tech/venv WSGIProcessGroup gautam.tech #Your static files location Alias /static /var/www/html/gautam.tech/static Alias /media/ /var/www/html/gautam.tech/media Alias /static/admin/ /var/www/html/gautam.tech/static/admin <Directory /var/www/html/gautam.tech/static> Require all granted </Directory> <Directory /var/www/html/gautam.tech/media> Require all granted </Directory> WSGIScriptAlias / /var/www/html/gautam.tech/myproject/wsgi.py DocumentRoot /var/www/html/gautam.tech <Directory /var/www/html/gautam.tech> <Files wsgi.py> Require all granted </Files> </Directory> CustomLog /var/www/html/gautam.tech/access.log combined ErrorLog /var/www/html/gautam.tech/error.log </VirtualHost> 

This will work for sure!

1 Comment

Very good answer. I only had to run the collectstatic command.
4

Deploy serving static content in production enviroment.

Assuming you are deploying using Gunicorn https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/gunicorn/

First you need to debug using Inspect in Google Chrome to see what gone wrong. Right click, chose the inspection and then choose the network section.

Case 1: Your website don't return the css file.

Web server don't serve static file

This means you haven't set up the web server right. You need to configure a web server for serving static contents. The guide from Django (and myself) suggests using NGINX https://docs.djangoproject.com/en/5.1/howto/static-files/deployment/#serving-static-files-from-a-dedicated-server.

Step 1: Install and configure NGINX to serve static files. See https://nginx.org/en/docs/beginners_guide.html#static and https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/

Step 2: Collect all static files into a single folder. Follow the instructions in https://docs.djangoproject.com/en/5.1/howto/static-files/#deployment

Setting STATIC_ROOT to the directory you want to serve file.

Run python manage.py collectstatic to collect all the static file in your apps to the directory STATIC_ROOT.

Note that the directory for serving files in NGINX and STATIC_ROOT should be the same. For example, For example, STATIC_ROOT = "/var/www/example.com/static/ in settings.py and in NGINX config file, it is root /var/www/example.com/static/.

Case 2: The static file serving works perfectly when using development servers (when using the command python manage.py runserver 0.0.0.0:8000), but when you use NGINX to serve file, the css file was serving successfully, but the admin site still does not have css.No css loading despite css return

This was because the the Content-Type of the served css file is text/plain where it should be text/css, as in the below image.

Wrong Content-Type css file

In order to fix it, you need the web server to serve the correct Content-Type of the css file. In my case, it is NGINX.

Adding include mime.types to the static file servings path should solve the problems. THe NIGNX configuration should be:

 location /static { include /etc/nginx/mime.types; root /var/www/example.com/static; } 

And voila !!!

it worked

Note: Be careful with cache file when debug, check the file was served with access log if possible.

TLDR: Check if the static file serving works, then check for its Content-Type

Comments

3

Ensure that 'django.contrib.staticfiles' is in your INSTALLED_APPS in your settings.py

Comments

3

In addition to many of the other answers being useful, I had a problem that hasn't yet been noted. After upgrading from Django 1.3 to 1.6, my static files directory had a broken symbolic link to the django admin static files.

My settings.py was configured with:

STATICFILES_DIRS = ( '/var/www/static/my-dev', ) 

According to this answer,

Django will now expect to find the admin static files under the URL /admin/.

I had a symbolic link /var/www/static/my-dev/admin which was set to:

admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/ 

That location no longer exists in django 1.6, so I updated the link to:

admin -> /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/ 

And now my admin site is working properly.

2 Comments

I have a simple project with simple files. My css loads when running on local, but when I hosted my website the css doesn't load, kindly help.
@user14528515 I suggest you open a new question for the site in general to address. Also, after 9 years, I've moved on to other domains.
3

In the issue is in a dev/test/prod server and using Nginx, please follow the below steps.

  • set the configs in settings.py as something below

    STATIC_URL = '/static/' BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_ROOT = os.path.join(BASE_DIR, 'static') 
  • Run the below command to create css and js files in static folder

    $ python manage.py collectstatic 
  • config in /etc/nginx/sites-enabled/example (Nginx) to serve static files

    location /static/ { alias /project/root/folder/static/; } 

Comments

2

If you are using Apache server to host your django site, you need to make sure the static alias point to your /directory to site/site_media/static/. If your static files are in /directory to site/site/site_media/static/, the previous Apache alias configuration will not work.

Comments

2

While following the Django tutorial, I had a similar problem and in my case the issue was the mimetype used by the development server when serving css files.

The mimetype served was 'application/x-css' which led to following warning message in Chrome (in the 'Network' tab of the Developer tools):

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

The workaround that I've found: changing the mimetype to be served by adding following lines to the django webapp's manage.py file:

import mimetypes mimetypes.init() mimetypes.types_map['.css'] = 'text/css' 

Note: worked for me with Django 1.7.4 on Python 2.7 and Chrome 40.0

1 Comment

tried this with Django 2.2.1 and it does not work. My admin panel static .css files are being served by nginx with HTTP code 200, but Chrome says they are coming in as Content-Type: text/plain, and they are not being rendered on the page.
2

Same sort of issue i encountered while developing a site in django-1.10.5 and python-2.7.13. But in my firefox-51 and chrome, the login page was able to get the css but still there was no styling. But weirdly it was working on IE-8..

I tried do every possible thing mentioned here and suitable to my set of sw versions. None worked.

But when i tried the same site on other system which had the python-2.7.8, it worked..

Just posted if it may help someone...

edited: later I found that in python-2.7.13, writing the following two lines in settings.py (plus clearing the cache of the browser) had done the trick

import mimetypes mimetypes.add_type("text/css", ".css", True) 

Comments

2

My issue was resolved by creating new Virtual Environment for the project, before that I was using general system level python interpreter.

$ mkvirtualenv myproject

Reference: https://docs.djangoproject.com/en/2.1/howto/windows/

Comments

2

this works fine and easily. I moved (manually) the folder. just you have to copy your static/admin from the directory of the main Project and paste it into public_html static/ if there is no static folder you have to run following command in terminal

python manage.py collectstatic 

here you go with css working of Django admin

Comments

2

If anyone experiencing same issue with Django and have installed 'django-compresor' package, go to settings.py and change this

STATICFILES_FINDERS = ( "compressor.finders.CompressorFinder", ) 

to this

STATICFILES_FINDERS = ( "compressor.finders.CompressorFinder", "django.contrib.staticfiles.finders.FileSystemFinder", "django.contrib.staticfiles.finders.AppDirectoriesFinder", ) 

Explanation: django-compressor is a package that you install when you want to minimize your source files, but it will sometimes override your settings.py configurations so you need to check your configuration and make some adjustments to make the magic work

1 Comment

This worked for me, Thank you
1

Admin panel was working fine except css wasn't loaded. This worked for Lightsail Django with Apache

1.Define STATIC_ROOT and STATIC_URL in settings.py

STATIC_ROOT = '/opt/bitnami/projects/decisions/decision/' STATIC_URL = '/static/' 

2.Eject(copy) admin assets files to the project

run python manage.py collectstatic this command creates /opt/bitnami/projects/decisions/decision/admin folder with css/ fonts/ img/ js/ subfolders

3.Make /static url accessible from apache

Paste this snippet in /opt/bitnami/apache2/conf/bitnami/bitnami.conf (If you have set up ssl then the file location will be /opt/bitnami/apache2/conf/bitnami/bitnami-ssl.conf)

Alias /static/ "/opt/bitnami/projects/decisions/decision/" <Directory "/opt/bitnami/projects/decisions/decision/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> 

4. Don't forget to restart apache

sudo /opt/bitnami/ctlscript.sh restart apache 

Comments

1

Check your settings.py file

STATIC_URL = '/static/' 

there should be backslash ' / ' in both opening and closing side ..

Comments

1

I had to copy the files from the site-packages over to static manually.

cp -r lib/python3.10/site-packages/django/contrib/admin/static/admin static 

collectstatic not doing this for me. It says 0 files copied.

Comments

0

Failing after trying 1000s of suggestions, I finally found a solution that helped. Here is what I tried and what I was using. I am using django-1.11 and nginx web server. Firstly, I made sure that my CSS/js files are not getting 404 in browser's console. After that, I could see a warning

Resource interpreted as Stylesheet but transferred with mime type text/plain

I found the base.html in admin templates and removed

type="text/css"

and now the lines looks like this:

<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" /> 

This fixed the issue for me.

Comments

0

If you have a value set in settings.py for STATICFILES_DIRS and the declared folder doesn't exist or is in the wrong location, it will cause the Admin to have no styling e.g. by defining:

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

And the static folder doesn't exist .

Comments

0

Configuring static files

Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.

In your settings.py file, define STATIC_URL, for example:

STATIC_URL = '/static/' 

For more details see static files [django-docs]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.