Overriding "Add user" header in django admin - django

How can I change the text at the top of the add user page? I am talking about the text in this section:
I've been reading the docs and searching through the code, but I'm not finding the place to override this.

This text is defined in the admin/auth/user/add_form.html template. To override it create your own template with the same name in your project.
EXAMPLE:
{% extends "admin/change_form.html" %}
{% block content_title %}
<h1>This is the title</h1>
{% endblock %}
{% block form_top %}
<p>This is the subtitle</p>
{% endblock %}
{% block after_field_sets %}
<script type="text/javascript">document.getElementById("id_username").focus();</script>
{% endblock %}

Related

Reuse same "block" of html in multiple django templates

Currently, I have two html template that extends from a base.html:
page1.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
Code_block_1
{% endblock %}
page2.html:
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
Code_block_1
{% endblock %}
Both html share the same Code_block_1.
I was thinking about about creating another html called Code_block_1.html to consolidate this repeating piece of code. Then, insert Code_block_1.html into page1.html and pag2.html. Django only lets you extend once. How do I get around this problem?
Thanks.
Simply create another HTML file called code_block_1.html and then inside both page1.html and page2.html use include like this:
<!-- page1.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard1 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}
<!-- page2.html -->
{% extends 'dashboard/base.html' %}
{% block tittle %} Dashboard2 {% endblock %}
... code ...
{% include 'code_block_1.html' %}
{% endblock %}

Djangocms template not showing up

I have a basic DjangoCMS up and running.
base.html contains:
{% block content %}{% endblock content %}
I also have feature.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block title %}{% page_attribute "page_title" %}{% endblock title %}
{% block content %}
<div>
{% placeholder "feature2" %}
</div>
<div class="jumbotron"">
{% placeholder "feature" %}
</div>
<div>
{% placeholder "content" %}
</div>
{% endblock content %}
I added the "feature2" placeholder in the above, and it correctly displays for editing on the site.
I then added a new line to base.html:
{% block base_logo %}{% endblock base_logo %}
and created a new file, base_logo.html:
{% extends "base.html" %}
{% load cms_tags %}
{% block base_logo %}
<div>
{% placeholder logo %}
</div>
{% endblock base_logo %}
I expected this to also appear on the site for editing, but it doesnt. I have added the base_logo.html to the CMS_TEMPLATES in settings.py and TEMPLATE_DIR is also pointing correctly.
What else do I need to do for Djangocms to pick up my new template?
Take a look at template inheritance.
You're trying to use two {% extends %} tags, which won't work. You should use the {% include %} tag for base_logo, because it seems you'd want to include this in many templates. This question provides more info.

How to extend a base.html using two different files?

I'm writing an application in which user can choose one of several tools for data analysis and open it in a panel on main page. Is it possible to use django "extends" and have each tool defined in different file?
The minimal example of what im strugling with would be like this:
base.html
<div>
{% block left_panel %}
left block
{% endblock content%}
</div>
<div>
{% block right_panel %}
right block
{% endblock %}
</div>
and sample left_panel and right_panel tools:
left1.html
{% extends "base.html" %}
{% block left_panel %}
<p>TEST left 1</p>
{% endblock %}
right1.html
{% extends "base.html" %}
{% block right_panel %}
<p>TEST right 1</p>
{% endblock %}
Is there a way to render the base.html with both blocks overwriten?
I believe that the best way to implement your requirement is to create a new template that extends base.html and includes left1.html and right1.html. Something like this:
{% extends "base.html" %}
{% block left_panel %}
{% include "left1.html" %}
{% endblock content%}
{% block right_panel %}
{% include "right1.html" %}
{% endblock %}
Update based on OP's comment: Actually you just need one configurable template, not 100. Let's say that based on the tools the user selects, your view passes the left_tool and right_tool context variables to your template. Now, you can easily do something like this:
{% block left_panel %}
{% if left_tool == "tool1" %}
{% include "left1.html" %}
{% elif left_tool == "tool2" %}}
{% include "left2.html" %}
etc ...
{% else %}
{% include "left10.html" %}
{% endif %}
{% endblock content%}
You'll do the same with the right panel. Of course the above is a little naive and definitely not DRY -- instead you could for instance generate the name of the template to be included in the view and pass it directly to the template, or use a custom node etc.

django - copy header from admin to all templates

So what I want to do is add the django admin header within my own base template for my project. I copied over the base.html from admin templates to my project. Can I somehow put {% block header %} tags within base.html and then call it within my own base template for my project?
{% block header %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% if user.is_active and user.is_staff %}
<div id="user-tools">
{% trans 'Hi,' %}
<strong>{% filter force_escape %}{% firstof user.first_name user.username %}{% endfilter %}</strong>.
{% block userlinks %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% trans 'Change password' %} /
{% trans 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% endblock %}
All templates that extend from 'base.html' would contain the content inside {% block header %} and {% endblock %}, as long as they do not override the block or its ancestors(by removing the {% block header %}...{% endblock %} part).
If there are templates that do not extend from base.html, you could put the code into their common base, or use something like inline tag or inclusion tags
Also, for the code to work properly for authenticated users, you need to ensure there is user variable in context: normally it's already there, or you need to re-enable "django.contrib.auth.context_processors.auth" if you've removed it before, check the doc
I ended up extending my main template from the admin 'base.html' template and going from there. A little messy but it works

Issue trying to customizing the admin in Django

In order to add some links in the admin of my site I added a custom block (surrounded in red in the images below) to admin/base.html and set it in admin/base_site.html.
The issue is that it's shown in all admin pages (eg connexion screenshot shown below), while I'd like to show it only in the site admin first page.
Anybody could help?
admin/base.html
...
<!-- 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 %}
{% block myblock %}{% endblock %} <!-- custom block -->
<br class="clear" />
</div>
<!-- END Content -->
....
admin/base_site.html
....
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'Administration de Django' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
{% block myblock %}
<div style="margin-top:160px;">
<div style="font-size:18px; color:#666666;font-weight:bold;margin-bottom:10px;">Rapports</div>
Rapports journaliers<br/>
Rapports mensuels
</div>
{% endblock %}
....
Site admin
Connexion
Then you are better off overriding admin/index.html
For this purpose I would like to recommend to you django-admin-tools application. As documentation says:
django-admin-tools is a collection of extensions/tools for the default
django administration interface, it includes:
a full featured and customizable dashboard;
a customizable menu bar;
tools to make admin theming easier.
Please join the mailing list if you want to discuss of the future of django-admin-tools.