I have downloaded the following templates from admin.contrib: base.html, base_site.html and index.html.
I would like to extend index.html, but no matter what I do it does not seem to work.
In settings.py, I have declared the name of my app to override the order in which the admin files are render. So, I can edit the actual files and content does change when I run the server.
But, I create a file in the directory templates/admin/foo.html.
The code with foo.html is:
{% extends "admin/index.html" %}
{% block something %}
<p>Hello</p>
{% endblock %}
And I have change the file templates/admin/index.html to have {% block something %}{% end block %}
When I run the server, the content of foo.html does not display.
Do I need to add foo.html to my urls.py and create a function within views.py to render the content?
Any help would be greatly appreciated.
Foo.html is not recognized by Django admin. If you want to customize admin/index.html, do your changes in admin/index.html.
If you want to use a separate file, include it inside the admin/index.html like this:
{% include "foo.html" %}
Hope it helps!
Related
Is there a way to extend base.html file in python django by default, I want it because I am overwriting {% extends %} tag in every html file
No but what you can do however is created a new HTML file called for example, header-tags.html with something like that :
{% extends "base.html" %}
{% load static %}
.... more tags
And then include this snippet wherever you want with {% include 'header-tags.html' %}
I've a base.html file which has vertical and horizontal menu-bar:
Wherever I want to use that I just simply write:
{% extends 'base.html' %}
{% block content %}
//html code
{% endblock content %}
But I don't know how to use the same file base.html from templates directory in djando admin.
I want output like this:
What I Tried:
How to override and extend basic Django admin templates?
How do I correctly extend the django admin/base.html template?
Override Navbar in Django base admin page to be same as the base.html
I tried few other solution just don't want to increase the length of question and base.html file's code just has basic bootstrap, html code for menus.
I am new to Django, little explanation would be highly appreciated!
What you are looking is similar to nav-global.
Try this:
First create a folder in your templates folder as admin and create a html file(base_site.html) in the same folder
Assuming you have separate html file for menu-bars(Let's say the file is nav.html).
Write the below code in base_site.html:
{% extends 'admin/base.html' %}
{% block nav-global %}
{% include 'nav.html' %} #Your navigation html file
{% endblock %}
Unrelated to question: I found a git repo which will give you idea how to customize the django-admin menu.
You can just extend the admin's base template as
{% extends "admin/base.html" %}
For example:
{% extends "admin/base.html" %}
{% block sidebar %}
{{ block.super }}
<div>
<h1>Extra links</h1>
My extra link
</div>
{% endblock %}
Also, make sure that you have added the admin app to the INSTALLED_APPS
INSTALLED_APPS = [
# other apps,
'django.contrib.admin',
# other apps,
]
I had the same issue about a year and a half ago and I found a nice template loader on djangosnippets.org that makes this easy. It allows you to extend a template in a specific app, giving you the ability to create your own admin/index.html that extends the admin/index.html template from the admin app. Like this:
{% extends "admin:admin/index.html" %}
{% block sidebar %}
{{block.super}}
<div>
<h1>Extra links</h1>
My extra link
</div>
{% endblock %}
I originally thought I would use {% extends ["filename.html"] %} to take html I've written in a template and put it into my main file, but since you can only use "extends" once, how can I have it so that my sidebar.html and slider.html are like snippets that can be put into my main html file, which I've called list.html
Here's an image of what things look like now: http://imgur.com/K5XKAcQ
Here's an image of the error: http://imgur.com/Uqs3iQo
Use "extends" like referencing a base class. Multiple-inheritance isn't supported (directly). To include snippets of code in your content, use "include":
{% extends "layout.html" %}
{% include "sidebar.html" %}
{% include "slider.html" %}
See: https://docs.djangoproject.com/en/dev/ref/templates/builtins/
You can use include in the main template
Rather than adding a tag in the sidebar.html, in filename.html, add:
{% include "sidebar.html" %}
You can use an include statement. Also, see here.
I'n trying to create inclusion tag so I can display data in my navigation bar on every page. The tag will be included in the "base.html" so that way it should display everywhere.
tags.py
#register.inclusion_tag('menu.html')
def show_hoods(HoodList):
gethoods = Hood.objects.all()
return {'gethoods': gethoods}
menu.html
{% for hood in gethoods %}
<h3>{{ hood.name }}</h3>
{% endfor %}
For some reason the menu.html template is blank and is not showing any data.
Also, once I have the menu.html running, will simple {% include 'menu.html' %} work inside the base.html? Will that be automatically rendered?
Edit:
Based on the feedback below, the code above is correct, however the base.html code was incorrect as the inclusion_tag is not loaded with {% include %} but {% load %} is used instead.
corrected base.html
{% load tags %}
{% show_hoods hoodlist %}
Thanks for the feedback!
Directly viewing the menu.html template will not display anything as it has no context variables set. gethoods will be empty so there will be nothing for the for loop in the template to loop over.
One of the main purposes of an include tag is to set extra context variables and then render a template using these variables. Directly viewing the template will show the template without the variables, but including the include template ({$ show_hood %} in your case) will add the context variables (gethoods) and render the template using them.
Answering your second question, you add include templates using their name, (the name of the function by default) rather than the {% include %} tag. The {% include %} tag is for when you simply want to render one template inside of another, and where it either doesn't need any context variables or uses the context variables available to its parent template.
I'm trying to add features to Django 1.2 admin's main page.
I've been playing with index.html, but features added to this page affect all app pages.
Any ideas on what template I'm supposed to use?
Thanks loads!!
You can use template hierarchy like:
index.html
...
{% block content %}
...
{% block mycontent %}My custom text{% endblock %}
...
{% endblock %}
app_index.html
...
{% block mycontent %}{% endblock %}
..
According to http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template you will want to override admin/app_index.html
I have done this by modifying the admin/index.html template. You may also need to modify admin/base_site.html (depending on what you want to do, exactly).
These templates are found in the django/contrib/admin/templates/admin folder in a Django installation.
Update: That's exactly what I've done, see the screenshot fragment below. The section marked in red is the section I added, via HTML in admin/index.html. However, you don't say which version of Django you're using - my example is from a 1.0 installation.