adding custom CSS to django admin forms - python

I'm trying to style a form in django admin. I created a file to extend the base admin page with myApp/templates/admin/base_site.html and I'm calling in a new css file with:
{% extends 'admin/base.html' %}
{% load static %}
{% block branding %}
{% block extrastyle %}
<link rel='stylesheet' href='{% static 'css/admin.css' %}'>
{% endblock %}
{% endblock %}
I added a static directory and file at myApp/static/admin/css/admin.css. Since the admin app has already took control of URIs like http://127.0.0.1:8000/static/admin/css/base.css my app can't find static/css/base.css because it expects my admin.css to be in venv/lib/python3.9/site-packages/django/contrib/admin/static/admin/css. I want to extend the base css (add to it), not necessarily completely override it.
I'm guessing the answer is to edit settings.py to tell it to look at myApp/static but I'm not sure how. The relevant settings.py lines are:
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static/'),
]
STATIC_URL = 'static/'
Thanks.

Related

Edit the template of admin top page

I am editing the template of admins
I successfully override the each model's edit page.
/myapp/template/admin/modelA/change_list_results
/myapp/template/admin/modelB/change_list_results
However how can I override the top of admin??
After login, there is a application and table lists.
I tried to override these below from /django/contrib/admin/templates/admin/ folder
/myapp/template/admin/app_index
/myapp/template/admin/index
/myapp/template/admin/base
However , still in vain.
Try to override base_site.html and include base.html using extends. I use this method to override branding.
tempaltes/admin/base_site.html
{% extends "admin/base.html" %}
{% block branding %}
<h1 id="site-name">
Name of the site
</h1>
{% endblock %}

`Django administration` to the custom title in project templates

In django admin panel, I have a requirement, how can I change the:
Django administration to the custom title?
I followed a post, but it do not give the clear steps.
I copy the django/contrib/admin/templates to my project /templates/admin, and changed the /templates/admin/base_site.html:
<h1 id="site-name">{{ site_header|default:_('Django11 administration') }}</h1>
You see, I changed the Django to Django11, but however in the browser, it do not change at all.
The other post on stackoverflow steps are punch-drunk, so who can tell me what should I do more to my requirement? is there need any configurations of my project settings? or something else?
No need to copy and change admin template
In project urls.py just put
admin.site.site_header = 'Mysite Admin Panel'
admin.site.site_title = 'Mysite Admin Panel'
Alternatively:
After copying django/contrib/admin/templates/admin/base_site.html put like this
base_site.html
{% extends "admin/base.html" %}
{% block title %}Mysite Admin Panel{% endblock %}
{% block branding %}
<h1 id="site-name">Mysite Admin Panel</h1>
{% endblock %}
{% block nav-global %}{% endblock %}

HTML changes not reflected in django-cms

I was working on an old django-cms project and was trying to edit base.html file and no changes are reflected when I reload the page.
But if I delete all the lines in that file the django runserver refuses to start showing error
django.core.exceptions.ImproperlyConfigured: The 'js' and 'css' sekizai namespaces must be present in each template, - or a template it inherits from - defined in CMS_TEMPLATES. I can't find the namespaces in 'project/cms/home.html'.
They why isn't other changes like adding a new class not reflected in the page reload or server restart.
NOTE:
The project is working good as it is. I was trying to modify it a little bit. Changes I made in the css pages are getting reflected when I reload the page. Issue is only when I try to edit HTML pages
For base.html, you need to have {% load cms_tags sekizai_tags %} in the file. Add {% render_block "css" %} to <head></head> and {% render_block "js" %} somewhere between <body></body>. Depending on the template files that inherit from base.html, certain portions may have been overwritten. For example, if you had:
{# base.html #}
{% block content %}
<div class="example-class"></div>
{% endblock %}
But in another file say:
{# layout.html #}
{% extends "base.html" %}
{% block content %}
{% endblock %}
The div would not appear.
If however, you are talking about missing CSS files, you still need to include them in <head> for it to be displayed. render_block "css" is for django-cms css files that are included in plugins etc. I usually use a LESS or SCSS compiler to include CSS into my projects.
Hope that helps. Post more details for a better diagnosis.

Is it possible to load a custom template tag in base and use it in extented templates

I loaded a custom template tag note_extras.py in base.html.
base.html
<div id="wrap">
{% load note_extras %}
{% block content %}
{% endblock %}
</div><!--wrap-->
but it is not accessible at templates which is an extend of base.html ie::
home.html
{% extends "base.html" %}
{% block content %}
<div class="container">
{% create_tagmenu request.user.pk %}
</div>
{% endblock %}
it is working fine if i load note_extras in home.html ie:
{% extends "base.html" %}
{% load note_extras %}
....
In Django template language, you must load all needed template libraries in each of the templates.
I personally think it is a good idea because it makes templates more explicit (which is better than implicit). Ill give an example. Prior to Django 1.5, the default behavior for a url tag was to provide the view name in plaintext as well as all the needed parameters:
{% url path.to.view ... %}
There however was no way to provide the path to the view via a context variable:
{% with var='path.to.view' %}
{% url var ... %}
{% endwith %}
To solve that, starting with 1.3, you could import the future version of the url tag (which became the default in 1.5) by doing:
{% load url from future %}
{% url var ... %}
or
{% url 'path.to.view' ... %}
Now imagine that you would need to create a template which would extend from a base template which you did not create (e.g. one of django admin base templates). Then imagine that within the base template it would have {% load url from future %}. As a result, {% url path.to.view ... %} within your template would become invalid without any explicit explanation.
Of course this example does not matter anymore (starting with 1.5) however hopefully it illustrates a point that being explicit in templates is better than implicit which is why the currently implementation is the way it is.
If you want that a template tag is loaded in every template you want to do it in the init file of your app:
from django.template.loader import add_to_builtins
add_to_builtins('my_app.templatetags.note_extras')
In case anyone was wondering, add_to_builtins has been deprecated but one could still load a tag for all of the templates in the project via settings.TEMPLATES - supported for Django 1.9 onwards as described here:
https://stackoverflow.com/a/59719364/2447803

Getting different django administration title

I am trying to get a different header in django administration. I would like to put the company's name there instead. I am trying to do it through the docs. https://docs.djangoproject.com/en/1.5/intro/tutorial02/
Near the bottem, it says to add a TEMPLATE_DIRS setting, which I did.
So, if I have:
'/LPG/firstproject/firstproject/templates',
in my TEMPLATE_DIRS
and this is where the django source file of base_html is at
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/
What exactly does it mean when it says "Now copy the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself"
Is this done with a command or how exactly do I do this?
Try this:
Inside your main template folder, create an admin folder. Inside it, create a file named base_site.html with the following content:
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}
{{ title }} | {% trans 'Your Site Title' %}
{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Your Site Title' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
Basically, if you want to override a django admin template, you have to match the path for the template and then create your own custom template.

Categories