Getting different django administration title - python

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.

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

Django template tag argument count error

I have a project running on Django 1.6 that is being developed on local machines with a git repo on BitBucket.
We have some test environments running on a server where we then checkout the repo and run the software.
When loading the login page one of the test environments presents the following error:
TemplateSyntaxError at /login/
breadcrumbs requires 0 arguments, 1 provided
In template /.../templates/includes/breadcrumbs.html, error at line 6
The project contains the following code:
admin/login.html
{% extends "workflow/base_site.html" %}
<!-- ... -->
{% block breadcrumbs %}{% endblock %}
workflow/base_site.html
{% extends "workflow/base.html" %}
<!-- ... -->
{% block breadcrumbs %}
{% include "includes/breadcrumbs.html" %}
{% endblock %}
breadcrumbs.html
{% load tags %}
<div class="breadcrumbs">
{% if crumbs %}
{{ crumbs | breadcrumbs:current_object_id | safe }}
{% endif %}
</div>
tags.py
from django import template
register = template.Library()
#register.filter
def breadcrumbs(obj=None, current_object_id=None):
# ...
pass
The login.html template extends the workflow/base_site.html but overrides the breadcrumbs block, and shouldn't be including breadcrumbs.html.
For the login page the breadcrumbs.html template is not expected to have a context variable named crumbs, and shouldn't be calling the breadcrumbs() tag.
The errors are only occurring in one of out test environments, i.e. the same code runs on the same machine under a different user account.
Is anyone able to identify the cause of this problem?
You might have another filter named breadcrumbs in your project. Try renaming yours to something else to check.
It can also be a compiled .pyc file, as you later found out.

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

Django-cms placeholder in cms template breaks page

So I moved my cms templates to a /cms folder in the templates folder:
templates/cms/default_template.html
I edited the settings.py as follows:
CMS_TEMPLATES = (
('cms/default_template.html', 'Default Template'),
)
My template looks like this:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_content %}
{% placeholder template_1_content %}
{% endblock %}
Problem is it throws the following error on page load:
TemplateDoesNotExist at /en/
Error during template rendering
3
4 {% block base_content %}
5 ***{% placeholder template_1_content %}*** -> This line is red indicating the problem is here
6 {% endblock %}
If I remove the {% placeholder template_1_content %} the page loads fine no problems.
It's only after adding that placeholder that it goes bonkers for no reason I can think of.
Has anyone seen something like this before?
BTW the page will load with the placeholder if I don't use the /cms folder.
So what is it about the placeholder and the folder location???
Any help would be appreciated.
I figured it out. Turns out the name of the first template created was stored in the database and that was being referenced. I had to manually edit the database to the new location and name of the template

Categories