Django admin header customisation with my software release - django

I have in version of my software (settings.release=1.0.14) and I want to show this info in django admin header under or after the product logo, but no idea how to do it.
My base_site.html is:
{% extends "admin/base.html" %}
{% load static %}
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name">
<a href="{% url 'admin:index' %}">
<img src="{% static 'lumbara.png' %}" height="50px" />
</a>
</h1>
{% endblock %}
{% block extrastyle %}
<style>
.module h2, .module caption, .inline-group h2,#header
{
/*margin: 0;*/
/*padding: 2px 2px 2px 2px;*/
/*font-size: 12px;*/
/*text-align: left;*/
/*font-weight: bold;*/
background: #006400 url(../img/default-bg.gif) top left repeat-x;
color: #fff;
}
</style>
{% endblock %}
{% block nav-global %}{% endblock %}

You have to write your own context_processors which returns a dictionary with your release variable. Then add it to the TEMPLATE settings, and you can use this variable in your templates - look at this answer for more info.

Related

Hide Django logo/name in header Django admin tool

I want to hide/change the name displayed in the header bar, the label that I want to change is "DJANGO" word as the next image shows"
Another solution for customizing the admin header.
Just copy /django/contrib/admin/templates/admin/base_site.html from django source (Link Here) and paste it under your templates directory.
For example,
your_project/templates/admin/base_site.html
Now you can change whatever you like in this template.
Hope this helps.
The easiest way is to simply add the following lines to your main urls.py:
admin.site.site_title = 'My Heading'
admin.site.site_header = 'My Heading'
admin.site.index_title = 'My Heading'
See the Django documentation for more attributes.
make a custom template file like:
templates/admin/base_site.html
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}YOUR WEB TITLE{% endblock %}
{% block branding %}
<h1 id="site-name">Your Site Name</h1>
{% endblock %}
{% block extrahead %}
<style type="text/css">
#header #branding h1{
background: None; // here you remove the django image
}
</style>
{% endblock %}
Just Share improve with favicon icon :
{% extends "admin/base.html" %}
**{% load staticfiles %}**
{% block title %}BROKR System{% endblock %}
{% block extrahead %}
**<link rel="shortcut icon" href="{% static 'img/logo1.png' %}"/>**
{% endblock %}
{% block branding %}
<h1 id="site-name">BROKR System</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
You will need to locate your
django/contrib/admin/templates/admin/base.html
This might be located in your
env/lib/site-packages
Thus, you find it in
env/lib/site-packages/django/contrib/admin/templates/admin/base.html
This is if you have virtual environment setup.
Else it will be in your base project folder containing your settings.py, wsgi.py etc
In:
your_base_folder/lib/site-packages/django/contrib/admin/templates/admin/base.html
Then go to line 32-34
<div id="branding">{% block branding %}{% endblock %}</div>
Replace the below with your img or text or h1
{% block branding %}{% endblock %}
For example:
<div id="branding"><img href='example.png'></div>
Or You could use the preferred django's
{{% url (bla.bla bla) %}}

Django template tag content missing when extending admin app base template

So I am creating a page in my Django project that essentially just uses the Django admin app header and footer.
I have a template folder in the root of my project where I have my base.html.
/templates/admin/base.html
{% load i18n static %}<!DOCTYPE html>
{% load static %}
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'js-stack/assets/css/spark-styles.css' %}" />
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}" />
{% block extrastyle %}{% endblock %}
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}" />{% endif %}
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
{% load i18n %}
<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
data-admin-utc-offset="{% now "Z" %}">
<!-- Container -->
<div id="container">
{% if not is_popup %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block usertools %}
{% if has_permission %}
<div id="user-tools">
{% block welcome-msg %}
{% trans 'Welcome,' %}
<strong>{% firstof user.get_short_name user.get_username %}</strong>.
{% endblock %}
/ View Analytics /
{% block userlinks %}
{% if site_url %}
{% trans 'View site' %} /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
{% trans 'Change password' %} /
{% endif %}
{% trans 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
{% trans 'Home' %}
{% if title %} › {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear" />
</div>
<!-- END Content -->
{% block footer %}<div style="padding: 10px 40px;" id="footer"><img src="{% static 'wifispark-logo.png' %}"></div>{% endblock %}
</div>
<!-- END Container -->
</body>
</html>
So this is a copy of the admin app base.html, but with a few modificatio0ns such as an image in the footer. This is all working as expected when I log into the admin app.
I also have another app called 'analytics' inside my project. Below is the template for it where I have extended the admin/base.html.
{% extends "admin/base.html" %}
{% load static %}
{% block content %}
<div class="container-fluid">
</div>
{% endblock content %}
Now, when I view my analytics page it loads with all the expected header and footer styling from the admin/base.html, but there is content missing such as the admin app title and the menu links, both of which are pulled through from admin app template tags.
Can anyone shed any light on what I am doing wrong or am missing?
Many thanks,
James
In your View, the one that renders the analytics page make sure you pass user, site_header, has_permission, and site_url as parameters.
These parameters can be extracted like this:
from django.contrib.admin import AdminSite
class MyAdminSite(admin.AdminSite):
pass
mysite = MyAdminSite()
And then return them while rendering your template:
def get(self, request):
return render(request, 'analytics.html', {'user': request.user,'site_header': mysite.site_header,'has_permission': mysite.has_permission(request), 'site_url': mysite.site_url})
Hope that helps

Django template rendering extend tag incorrectly

I have three Django templates:
base.html:
user_links.html
user_detail.html
I want user_links.html to extend base.html. Next, I want user_detail.html to extend user_links.html and base.html.
Here's base.html:
<head>
<title>Cool App</title>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/main.css" />
</head>
<body>
<h1>Cool App</h1>
<div class="navbar">
<p>
HOME |
{% if user.is_authenticated %}
LOGOUT
{% else %}
LOGIN
{% endif %}</p>
{% block content %}
{% endblock %}
{% block pagination %}
{% endblock %}</div>
Here's user_links.html:
{% extends "base.html" %}
Yellow
Pink
Green
{% block content %}
{% endblock %}
And here's user_detail.html
{% extends "user_links.html" %}
{% block content %}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}
So when the browser renders user_detail.html, I want it to (i) show the stylesheet and navigation links from base.html, (ii) show the word Yellow, Pink, Green from user_links.html, (iii) and show the user's username and bio. But (ii) is not being rendered at all, though (i) and (iii) are correctly rendering.
How should the templates be set up so that I see (i), (ii) and (iii) in user_detail.html? Please advise.
Note: all three templates reside in the same directory. I'm on Django 1.5
If you extends a base.html template, no content not surrounded by {% block %} will be rendered at all.
You could create additional {% block precontnet %}{% endblock %} in base.html, and wraps Pink/Yellow/Red in user_links.html
Or you can put Pink/Yellow/Red in {% block content %} if user_links.html and use {{ block.super }} in user_detail.html
links.html
{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}
user_detail.html
{% extends "user_links.html" %}
{% block content %}
{{ block.super }}
<h2>{{ object.username }}'s Profile</h2>
{% if object.userprofile.bio %}
{{ object.userprofile.bio }}
{% endif %}
{% endblock %}
Place div after </p> in base.html
<h1>Cool App</h1>
<div class="navbar">
<p>
HOME |
{% if user.is_authenticated %}
LOGOUT
{% else %}
LOGIN
{% endif %}</p>
</div>
Try this in user_links.html
{% extends "base.html" %}
{% block content %}
Yellow
Pink
Green
{% endblock %}

Django Admin Login Background

how can i change only background image of admin login site in django? i have tried some method but didnt achieved.
i have put base_site.html to under templates\admin.
what is next for coding about background image?
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
{% block extrastyle %}
<style type="text/css">
# body {background-color: #444444;}
</style>
{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Admin Login' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
The correct code is:
{% block extrastyle %}
<style type="text/css">
body.login {background-color: #444444;}
</style>
{% endblock %}
The problem was the # in the css which refers to an id, ie <some_tag id="body"> rather than <body>. body.login targets a body tag with a login class, ie <body class="login">, which is what the django admin site uses to identify the body of just the login screen.

Django - Admin - 'Add Object' button on Left side

By default, in the change list page, the 'Add Object' button is in right side. Is it possible to move it to the left side? Do I need to do any change in the following lines in change_list.html?
{% block object-tools %}
{% if has_add_permission %}
<ul class="object-tools">
<li>
<a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">
{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
</a>
</li>
</ul>
{% endif %}
{% endblock %}
{% block content %}
<div id="content-main">
<style type="text/css">
/* Moving the ADD buttons to the left side using CSS */
.object-tools {
font-size: 10px;
font-weight: bold;
font-family: Arial,Helvetica,sans-serif;
padding-left: 0;
float: left;
position: relative;
margin-top: 0.5em;
margin-bottom: -2em;
}
</style>
{% block object-tools %}
{% if has_add_permission %}
<ul class="object-tools">
<li>
<a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">
{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
</a>
</li>
</ul>
{% endif %}
{% endblock %}
<br> <br>