Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain to my why my django admin theme is dark?

I host my small project on pythonanywhere and after i host it i check if it is working and when i click the django admin, the theme of my django admin is dark and when i tried to run on my local host the theme is white so i tried to double check my static url and i think it is fine and btw this is my static url for my admin Static Url: /static/admin, Static Directory: /home/k3v1nSocialProject/.virtualenvs/myprojenv/lib/python3.8/site-packages/django/contrib/admin/static/admin. Can someone explain to me what is happening and why is my django admin theme is dark?

This is the actual photo of django admin dark theme

like image 926
k3v1n Avatar asked Apr 17 '21 05:04

k3v1n


People also ask

How do I change the admin color in Django?

Restart your application server and now you should be able to see a new color scheme added by the package. If you want to customize it further go to your admin and look for “themes” app and do customizations there. Rounded corners for a slight step towards “modern” design.

Can we change Django admin theme?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

What is the purpose of Django admin?

Overview. The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Is it possible to have a dark admin area in Django?

Currently, my admin area is in this state: As of Django 3.2, there is dark theme support which respects the system preference. Since your system is set to dark mode, it uses the dark theme for the admin. Most people would consider this to be a good thing as users that are used to dark don't get the light theme site, and vice versa.

Is it possible to add a dark mode theme in Django?

I am having a already da dark mode theme generator running. Now, the only page that appears bright is the django admin page.... As part of the Django 3.2 release, the admin now has a dark theme that is applied based on a prefers-color-scheme media query.

How to change the color scheme of the Django admin panel?

To change the color scheme of the Django admin panel we need to override the base admin template then include CSS rules to replace the original styling. In the root of your project source files create a directory called templates. From the command like you can use the mkdir command like this:

What is prefers-color-scheme media query in Django?

The prefers-color-scheme media query was introduced in Django 3.2 and is used to provide a dark or light theme depending on the color scheme in the users browser. Now you can edit the hex values of the above CSS properties to change the color of your admin panel.


Video Answer


4 Answers

From django 3.2 we have possibility to adjust admin themes. Fastest way to ignore Dark theme is:

Create admin folder inside your templates folder, then create file base.html

templates/admin/base.html

copy this code into base.html

{% extends 'admin/base.html' %}

{% block extrahead %}{{ block.super }}
<style>
/* VARIABLE DEFINITIONS */
:root {
  --primary: #79aec8;
  --secondary: #417690;
  --accent: #f5dd5d;
  --primary-fg: #fff;

  --body-fg: #333;
  --body-bg: #fff;
  --body-quiet-color: #666;
  --body-loud-color: #000;

  --header-color: #ffc;
  --header-branding-color: var(--accent);
  --header-bg: var(--secondary);
  --header-link-color: var(--primary-fg);

  --breadcrumbs-fg: #c4dce8;
  --breadcrumbs-link-fg: var(--body-bg);
  --breadcrumbs-bg: var(--primary);

  --link-fg: #447e9b;
  --link-hover-color: #036;
  --link-selected-fg: #5b80b2;

  --hairline-color: #e8e8e8;
  --border-color: #ccc;

  --error-fg: #ba2121;

  --message-success-bg: #dfd;
  --message-warning-bg: #ffc;
  --message-error-bg: #ffefef;

  --darkened-bg: #f8f8f8; /* A bit darker than --body-bg */
  --selected-bg: #e4e4e4; /* E.g. selected table cells */
  --selected-row: #ffc;

  --button-fg: #fff;
  --button-bg: var(--primary);
  --button-hover-bg: #609ab6;
  --default-button-bg: var(--secondary);
  --default-button-hover-bg: #205067;
  --close-button-bg: #888; /* Previously #bbb, contrast 1.92 */
  --close-button-hover-bg: #747474;
  --delete-button-bg: #ba2121;
  --delete-button-hover-bg: #a41515;

  --object-tools-fg: var(--button-fg);
  --object-tools-bg: var(--close-button-bg);
  --object-tools-hover-bg: var(--close-button-hover-bg);
}


</style>
{% endblock %}

Now you should have original colors back.

like image 89
TomRavn Avatar answered Oct 23 '22 16:10

TomRavn


As part of the Django 3.2 release, the admin now has a dark theme that is applied based on a prefers-color-scheme media query. Release Notes

The admin now supports theming, and includes a dark theme that is enabled according to browser settings. See Theming support for more details.

like image 36
Iain Shelvington Avatar answered Oct 23 '22 16:10

Iain Shelvington


To those wondering where to put this override data from Adam's response above, it would depend on where your TEMPLATES DIRS are assigned in the settings file. For example:

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [(os.path.join(BASE_DIR, 'templates')),], # <- Template path to put the html file
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Note the DIRS directory. This translates to a templates folder at the same level as my manage.py file.

Inside that templates folder i have another folder called admin and an html file called base. So it looks like this: \projectname\templates\admin\base.html

Then in the base.html file i have the code Adam mentions from the documentation theming support

{% extends 'admin/base.html' %}
    
{% block extrahead %}{{ block.super }}
<style>
    :root {
        --primary: #9774d5;
        --secondary: #785cab;
        --link-fg: #7c449b;
        --link-selected-fg: #8f5bb2;
        --body-fg: #333;
        --body-bg: #fff;
        --body-quiet-color: #666;
        --body-loud-color: #000;
        --darkened-bg: #f8f8f8; /* A bit darker than --body-bg */
        --selected-bg: #e4e4e4; /* E.g. selected table cells */
        --selected-row: #ffc;
    }

</style>
{% endblock %}

This should now work for you. If you use these exact settings here, it will be a light theme with purple. Then you can just accordingly.

like image 5
jAC Avatar answered Oct 23 '22 16:10

jAC


Django 3.2 admin theme change

If you want to return old theme as i did you can just override color variables.

Go to django/contrib/admin/static/admin/css/base.css and copy block that looks like this

/* VARIABLE DEFINITIONS */
:root {
  --primary: #79aec8;
  --secondary: #417690;  
  .......
}

Next create folder named admin in templates folder and create base.html file inside and place this code. Make sure that you replace :root with variables that you got from initial base.html

{% extends 'admin/base.html' %}
    
{% block extrahead %}{{ block.super }}
<style>
    :root {
      --primary: #79aec8;
      --secondary: #417690;
      --accent: #f5dd5d;
      --primary-fg: #fff;
      ......
    }

</style>
{% endblock %}

And enjoy old beautiful look of Django that we all like)

like image 2
Adam Avatar answered Oct 23 '22 17:10

Adam