why my django admin site doesn't have css style

I am making a Django admin site using the Django development version

But it has no CSS style:

alt text

What can I do?

thank

+68
css django admin
Dec 12 '10 at 4:15
source share
17 answers

Django does not serve static files on its own. You must indicate where the files are.

ADMIN_MEDIA_PREFIX in settings.py will point Django in the right place.

Since you are using the development version, you will need a dev-specific document for practical actions with static files . Adam Link will lead you to version 1.2.

+13
Dec 12 '10 at 19:39
source share

After setting STATIC_ROOT and STATIC_URL you may need to run

 python manage.py collectstatic 
+54
Apr 6 '12 at 18:29
source share

ADMIN_MEDIA_PREFIX now not recommended, use STATIC_URL . Setting STATIC_URL = '/static/' in settings.py should complete the task. Try:

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

and then:

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

Powered by Django 1.4 pre-alpha SVN-16920.

+16
Oct 02 '11 at 17:11
source share

I ran into this problem while also following the tutorial on the Django book. In chapter 5 | The installation of the model in the book is indicated when the default value is INSTALLED_APPS- "Temporarily comment out all six of these lines by placing a hash (#) before them." http://www.djangobook.com/en/2.0/chapter05.html

Then, in Chapter 6, the Book tells the reader to uncomment 4 of these 6 lines - “Notice that we commented on these four INSTALLED_APPS entries in chapter 5. Uncomment them now.”

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

+9
May 27 '13 at 19:41
source share

I read several other threads trying to fix this ... resorted to an alias, as in other threads. This assumes that your own user application is serving static files correctly, which indicates that your STATIC_ROOT and STATIC_URL have the correct 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 in this thread. : (

+8
Jul 19 '13 at 23:17
source share

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

Then set STATIC_ROOT as some folder in your file system that does not match any of your directories listed in the STATICFILES_DIRS list.

After setting STATICFILES_ROOT you run python manage.py collectstatic from the project directory.

This will copy all static admin files and all files to any other folders listed in the STATICFILES_DIRS list. It basically puts all your static files in one place so you can transfer them to a CDN when you deploy your site. If you are like me and you don’t have a CDN, you have two options:

  • Add the folder that you set as STATIC_ROOT to the STATICFILES_DIRS list. This will allow static file searches in django to find all static files.
  • Move the entire folder of static files to another location on your file system and hover STATICFILES_DIRS to include this new location.

I do not comment on security with this answer, this is exactly how I was able to develop using my web server for small projects. I expect you to need a CDN, as django will suggest if you are doing something more ambitious.

UPDATE: I just ran into this problem and this method didn’t quite do what I think you want. What ended up working for me was after I started collectstatic . I simply copied the static admin files, which he placed in STATICFILES_ROOT , into the directory that I used for my own static files. This solved the problem for me.

+3
Aug 15 2018-12-12T00:
source share

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

Settings.py settings have been configured with:

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

According to this answer ,

Now Django expects to find static admin files at the URL / admin /.

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

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

This place 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 correctly.

+3
Dec 05 '13 at 19:34
source share

If you are using an Apache server to host your django site, you need to make sure that the static alias points to your / directory on the site / site _media / static /. If your static files are located in the / directory on the site / site / site _media / static /, the previous Apache alias configuration will not work.

+2
Nov 02 '13 at
source share

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

The mimetype routine was "application / x-css", which led to the following warning message in Chrome (on the "Network" tab of the developer tools):

The resource is interpreted as a stylesheet, but is migrated with the MIME type application / x-css: " http://127.0.0.1:8000/static/admin/css/base.css "

Workaround I found: changing the mimetype type for maintenance by adding the following lines to the ddango webapp 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

+2
Feb 07 '15 at 16:29
source share

My problem was solved by creating a new virtual environment for the project, before that I used a general system-level Python interpreter.

 $ mkvirtualenv myproject 

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

+2
01 Oct '18 at 6:52
source share

Make sure 'django.contrib.staticfiles' is in your INSTALLED_APPS in your settings.py

+1
Aug 19 '13 at 21:43
source share

The same problem that I encountered when 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 css, but still there was no style. But strangely it worked on IE-8 ..

I tried to do my best, mentioned here, and is suitable for my set of sw versions. Nothing worked.

But when I tried the same site on another system that had python-2.7.8 on, it worked.

Just posted if this can help someone ...

edited: later I found that in python-2.7.13 writing the next two lines in settings.py (plus clearing the browser cache) did the trick

 import mimetypes mimetypes.add_type("text/css", ".css", True) 
+1
Feb 22 '17 at 18:28
source share

If you have the value set in settings.py for STATICFILES_DIRS and the declared folder does not exist or is in the wrong place, this will lead to the administrator having no style, for example, by defining STATICFILES_DIRS = (os.path.join (BASE_DIR, "static")) and the static folder does not exist

0
Jul 12 2018-02-17T00:
source share

After the failure of thousands of suggestions, I finally found a solution that helped. Here is what I tried and what I used. I am using django-1.11 and the nginx web server. Firstly, I made sure my CSS / js files don't get 404 in the browser console. After that I could see a warning

The resource is interpreted as a style sheet, but passed with the MIME type text / plain

I found base.html in admin templates and deleted

type="text/css"

and now the lines look like this:

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

This fixed the problem for me.

0
Sep 05 '18 at 12:06
source share

run: python manage.py collectstatic

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

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

Here are all the Vhost settings for configuring Django

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

It will work for sure!

0
Jul 09 '19 at 13:46
source share

This problem is located on the dev / test / prod server and uses Nginx, follow these steps.

  • set the configs in settings.py like 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 command below to create css and js files in a static folder

     $ python manage.py collectstatic 
  • Configuration in / etc / nginx / sites-enabled / example (Nginx) for serving static files

     location /static/ { alias /project/root/folder/static/; } 
0
Aug 22 '19 at 6:41
source share

it works well and easy. I moved (manually) the folder. just you need to copy your static/admin from the main project directory and paste it into public_html static/ , if there is no static folder, you must run the following command in the terminal

 python manage.py collectstatic 

here you go with django admin work CSS

0
Sep 28 '19 at 8:32
source share



All Articles