覆盖Django中的特定admin css文件

时间:2022-03-24 20:58:04

Overriding admin templates is as easy as creating a folder admin in your templates directory and copying whatever template files you'd like to override into it. I simply want to play with the admin style sheets however, so I made a folder admin in my static files folder and put css/base.css into it. Unlike the templates solution, this doesn't seem to work.

覆盖管理模板就像在模板目录中创建文件夹管理员一样简单,并将要覆盖的模板文件复制到其中。我只是想玩管理样式表,所以我在我的静态文件夹中创建了一个文件夹admin并将css / base.css放入其中。与模板解决方案不同,这似乎不起作用。

So is there any way to override individual css files for django.contrib.admin in the same way that you can override templates? If nay, what would be the best solution for overriding css files? I'm looking for a solution short of copying all the admin media files into my project and changing admin's static directory

那么有没有办法覆盖django.contrib.admin的单个css文件,就像你可以覆盖模板一样?如果可以,那么覆盖css文件的最佳解决方案是什么?我正在寻找一种解决方案,而不是将所有管理媒体文件复制到我的项目中并更改管理员的静态目录

3 个解决方案

#1


2  

I'm looking for a solution short of copying all the admin media files into my project and changing admin's static directory

我正在寻找一种解决方案,而不是将所有管理媒体文件复制到我的项目中并更改管理员的静态目录

I don't think there is really an alternative. You copy the media files into a new directory and while you start the server pass the adminmedia command line argument, like

我认为没有其他选择。您将媒体文件复制到一个新目录,并在启动服务器时传递adminmedia命令行参数,如

python manage.py runserver --adminmedia=./myadminmedia

In any case, when you run it on production server, the admin media has to be served from a good static serving server, for which, you can point this new path.

在任何情况下,当您在生产服务器上运行它时,必须从一个良好的静态服务器服务器提供管理媒体,为此,您可以指向此新路径。

Reference from the Docs: https://docs.djangoproject.com/en/1.3/ref/django-admin/#django-admin-option---adminmedia

文件参考:https://docs.djangoproject.com/en/1.3/ref/django-admin/#django-admin-option---adminmedia

#2


10  

What I'm doing to achieve that is to override base_site.html template like this:

我正在做的是覆盖base_site.html模板,如下所示:

{% block blockbots %}
<link rel="stylesheet" type="text/css" href="/media/css/my_admin.css" />
{{ block.super }}
{% endblock %}

I put the CSS in blockbotsinstead of extrahead to be sure that is loaded at the end, so it will override all others CSS.

我把CSS放在blockbots而不是额外,以确保在最后加载,所以它将覆盖所有其他CSS。

#3


0  

Probably you can hack your urls.py file and point only a single media URL to be served from a local folder while the others are served from the Django directory(in development mode).

可能你可以破解你的urls.py文件,只指向从本地文件夹提供的单个媒体URL,而其他媒体URL则从Django目录(在开发模式下)提供。

#1


2  

I'm looking for a solution short of copying all the admin media files into my project and changing admin's static directory

我正在寻找一种解决方案,而不是将所有管理媒体文件复制到我的项目中并更改管理员的静态目录

I don't think there is really an alternative. You copy the media files into a new directory and while you start the server pass the adminmedia command line argument, like

我认为没有其他选择。您将媒体文件复制到一个新目录,并在启动服务器时传递adminmedia命令行参数,如

python manage.py runserver --adminmedia=./myadminmedia

In any case, when you run it on production server, the admin media has to be served from a good static serving server, for which, you can point this new path.

在任何情况下,当您在生产服务器上运行它时,必须从一个良好的静态服务器服务器提供管理媒体,为此,您可以指向此新路径。

Reference from the Docs: https://docs.djangoproject.com/en/1.3/ref/django-admin/#django-admin-option---adminmedia

文件参考:https://docs.djangoproject.com/en/1.3/ref/django-admin/#django-admin-option---adminmedia

#2


10  

What I'm doing to achieve that is to override base_site.html template like this:

我正在做的是覆盖base_site.html模板,如下所示:

{% block blockbots %}
<link rel="stylesheet" type="text/css" href="/media/css/my_admin.css" />
{{ block.super }}
{% endblock %}

I put the CSS in blockbotsinstead of extrahead to be sure that is loaded at the end, so it will override all others CSS.

我把CSS放在blockbots而不是额外,以确保在最后加载,所以它将覆盖所有其他CSS。

#3


0  

Probably you can hack your urls.py file and point only a single media URL to be served from a local folder while the others are served from the Django directory(in development mode).

可能你可以破解你的urls.py文件,只指向从本地文件夹提供的单个媒体URL,而其他媒体URL则从Django目录(在开发模式下)提供。