I want to make a very small change to the Django admin logout page.
I know how to use templates to override the Django admin templates, so I have tried to do the same thing with the logout file.
I have set up a new template at templates/registration/logged_out.html. The content of this file is as follows:
{% extends "registration/logged_out.html" %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
However, something is definitely wrong, because when I try to log out of admin, the site stops running.
I've found the Django docs page recommending the use of AdminSite for changes to the base template and logout pages, but is this really necessary for such a tiny change?
If so, does anyone have an example of how I might set up the logout template? I'm rather intimidated by the instructions for AdminSite.
Thanks.
The reason of manage.py runserver termination is an inheritance loop.
Django loads "registration/logged_out.html" and that it tries to load it's parent: "registration/logged_out.html". Unfortunately parent is the same template and so we end up on the template inheritance loop.
Manage.py will terminate with some variant of stack overflow error...
You can easily escape the issue by extending the parent of original "registration/logged_out.html" -> "admin/base_site.html". I.e:
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block breadcrumbs %}<div class="breadcrumbs">{% trans 'Home' %}</div>{% endblock %}
{% block content %}
<p>Thanks for using the site.</p>
<p>Log in again</p>
<p>Return to the home page</p>
{% endblock %}
You're getting a template import loop. The template loader won't load the base template form wherever you've got Django installed, because it sees that you have that template in your project's template folder.
I think you'll need to copy the log out template from where you have Django installed to your project's template folder. Unfortunately that's the only way that seems to work. This method also means that if updates are made to the Django admin templates, you'll have to manually apply them to your modified templates.
Related
there is no suggestion for template tags or html tags what should i do ?
If you make sure that you've told pycharm that you're creating a django project;
Django Framework settings
Template language settings
Then bare in mind that django tags start with {% . Your template might look like this;
{% extends "base.html" %}
{% block content %}
<div class="row">
<div id="query_area" class="col-md-12">
<h2>Playground</h2>
<p>
The django docs for templates may be of use to you; https://docs.djangoproject.com/en/4.0/topics/templates/
Pycharm will automcomplete this templatetag when you type {% before the tag you want to use.
Pycharm will also autocomplete any custom tags you register in your applications
I'm trying to extend the password reset form like so:
<!-- templates/registration/password_reset_form.html -->
{% extends registration/password_reset_form.html %}
{% block content %}
<h1> hello </h1>
{% endblock %}
As far as I can tell, this should take the template from /usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/registration/password_reset_template.html (which exists, I checked) and replace the block content with the one at templates/registration/password_reset_form.html.
But this isn't happening. In fact, there is no change, nor is there an error. What am I doing wrong?
UPDATE:
I tried deliberately introducing a syntax error into the template name, and no error was issued. It leads me to believe that the template file is not being read at all. According to the django docs, registration/password_reset_form.html is the path to the default template. Why am I not able to at least introduce an error?
Put quotes around path, like this:
{% extends "registration/password_reset_form.html" %}
{% block content %}
<h1> hello </h1>
{% endblock %}
Also if your own template file didn't read at all, make sure that you have your app in INSTALLED_APPS in settings.py and TEMPLATES has 'APP_DIRS': True or DIRS set properly.
Why don't you just use the full path to the html file then?
/usr/local/lib/python3.7/site-packages/django/contrib/admin/templates/registration/password_reset_template.html
Right now you are extending the same file you are working on
My team and I are working on a project that involves a landing page and dashboard for the users after their login. We have more or less completed the functions of the webpages with very basic styling using bootstrap. We purchased a html template from the web and would like to 'transfer' what we have onto the new template so that it will look professional without us having to style everything from scratch.
I have very basic knowledge of the template inheritance system and thus have much difficulty using the purchased template into our work.
I would like to seek some guidance to how I can/should proceed to use the purchased template. For example should I work on the base.html using the purchased index.html file from the purchased template and how I should go about doing so.
I'm lost in how I should start so any tips will definitely be of a great help.
So the workaround is include all the css and js in static folder
static
css
js
templates
landing
homepage.html
base.html
copy all the index.html to base.html and remote inside body content and place it in homepage.html. Now
homepage.html
{% extends "base.html" %}
{% block content %}
paste all inside body contents here
{% endblock content %}
base.html
<body>
{% block content %}
{% endblock content %}
</body>
Note: Make your view return to landing/homepage.html
I'm trying to include a static html page from a django template.
I tried using {% include the_static.html %} but this doesn't work for some unknown reason.
the_static.html page is a data page that will be modified often with an html editor.
and my_model has a url to this html and include it. But django refuses to find it although I'm sure I've setup the path correctly.
You can write your custom template tag to do this.
Create a file named includestatic.py under appname/templatetags/. Also, remember to create appname/templatetags/__init__.py, to include the app in settings and to restart the server.
includestatic.py should have this code:
from django import template
from django.contrib.staticfiles import finders
from django.utils.html import escape
register = template.Library()
#register.simple_tag
def includestatic(path, encoding='UTF-8'):
file_path = finders.find(path)
with open(file_path, "r", encoding=encoding) as f:
string = f.read()
return escape(string)
To use it in your template, put {% load includestatic %} at the top of your template, and then use the tag like {% includestatic "app/file.txt" %}.
I am not sure I understand everything yet...
You've an HTML page served by Django on a given url, let's suppose it to be http://mydjangodomain/get_the_static/. This URL is set in the urls.py of your model. Ok, that's normal.
You have a django template for this model. Let's suppose it's defined in a template directory mytemplates/mymodeltemplates/ and it's called myfrontpage.html (since in Django templates are html files).
I guess you've an URL defined in your urls.py to server that front page ? Let's suppose it's http://mydjangodomain/get_the_front_page/
Now I don't understand how your front page use your static html. Do your final front page html need the static's URL for a "src" attribute or something like it, or do you need to include the static's html into the front page's html ?
In the 1st case, you already have the URL, it's http://mydjangodomain/get_the_static/ so just use it as if.
In the 2nd case, you don't need the previous URL, get ride of it. Furthermore, put the_static.html in mytemplates/mymodeltemplates/. Then you need the {% include "/mymodeltemplates/the_static.html" %} tag. If this doesn't work, make sure you've the following in your settings:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
APPLI_ROOT_PATH = "<absolute_path_to_the_application_root_on_your_server>"
TEMPLATE_DIRS = (
'%s/mytemplates' % APPLI_ROOT_PATH,
)
Sort of resurrecting the dead, but at least with django 1.10, there's a very clean answer here:
http://www.effectivedjango.com/tutorial/static.html
an excerpt from that page:
Simple Template Inclusion We want to add the Boostrap CSS to all of
our templates, but we’d like to avoid repeating ourself: if we add it
to each template individually, when we want to make changes (for
example, to add another stylesheet) we have to make them to all the
files. To solve this, we’ll create a base template that the others
will inherit from.
Let’s create base.html in the templates directory of our contacts app.
{% load staticfiles %}
<html>
<head>
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}"
rel="stylesheet" media="screen">
</head>
<body>
{% block content %}
{% endblock %}
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body>
</html>
base.html defines the common structure for our pages, and includes a
block tag, which other templates can fill in.
We’ll update contact_list.html to extend from base.html and fill in
the content block.
{% extends "base.html" %}
{% block content %}
<h1>Contacts</h1>
<ul>
{% for contact in object_list %}
<li class="contact">{{ contact }}</li>
{% endfor %}
</ul>
add contact
{% endblock %}
Having followed this exactly, I now have a base.html that includes all my style references and the navigation bars/etc, so the html in the block content is merely the central contents of each (varying) page.
Do you mean that you want to EXTENDS the parent template the_static.html?
If yes, you should add below code at the first line of your children template:
{% extends "the_static.html" %}
Details documentation can be found here
Is there a way to add custom views to the Django admin app?
To be specific, I would like to be able to switch out the "content" div for custom static content, while preserving the "header" and "footer" divs. This is different than redirecting to a static HTML page, which would not preserve the header and footer.
You just configure your view as usual (urls.py -> myapp.views) and then extend the admin base template:
{% extends "admin/base_site.html" %}
{% block content %}
My content looking like the rest of the admin app.
{% endblock %}
Here is a small intro http://www.djangobook.com/en/1.0/chapter17/, I think where is new version, but I think where is nothing changed in concept of extending admin pages.