Django Template Not Extending - python

I'm trying to extend a base template in Django named 'base.html' in my main page 'index.html' and it does not work as expected. Rather than extending the template, the page just displays {% extends 'base.html' %}, plus the HTML in index.html when displaying the index page.
The 'base.html' page sits in the root of my templates folder and my 'index.html' page sits in templates/pages.
In 'base.html':
{% load static %}
...some code...
{% block content %}
{% endblock %}
...some code...
In 'index.html':
{% extends 'base.html' %}
{% load static %}
{% block content %}
...some code...
{% endblock %}
In views.py:
def index(request):
return render(request, 'pages/index.html')
In settings.py:
TEMPLATES = [{'DIRS': [os.path.join(BASE_DIR, 'templates)]
Expected Result:
Navbar
Content
Actual Result:
{% extends 'base.html' %}
{% load static %}
{% block content %}
Content
{% endblock %}

Like others mentioned, you are most likely viewing the html file directly.
You need to go to a view that makes use of the template which will render the final html.
I am not familiar with your project structure, so try doing this first:
in urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='index.html'))
]
and copy your index.html to the root of your templates folder.
then browse to http://localhost:63342, once you have it working, then you can work your way out to suit your project structure.

Not opening the base.html template. The view function points to the the child data file. When I remove the extends statement from that, the child block data displays. So it is that Django is not interpreting the extends statement in the data file correctly when it exists. I can find no discussion of what might cause that to happen. And no error messaging. Kind of makes a deal breaker out of django.

You are probably opening the template itself rather than calling the development server. If Django has a problem with any of those things, it throws an error. It does not spit out the template code.

Related

Using variables across django templates

I have a link in a layout template that another template extends. i want the link to pass in a variable thats in the template that extends the other. I want to pass the name variable in the documentPage template through the editDoc link in the layout template. can anyone think of a way to do this? thanks
In views.py file, you can pass the link as a context dictionary to template:
def function_name(request):
generated_link = 'generated_link'
context = {
'link': generated_link
}
return render(request,"documentPage.html",context=context)
You can extends template as :
{% extends 'layout.html' %}
{% block body %}
# Write body here
Click Me
{% endblock %}

Django : Inheritance error- TemplateSyntaxError at / “Could not pass the remainder”

I have a website running using Django that uses the following dgango blocktag to render text within a base.html file.
{% block content %}
<h1>Welcome </h1>
<p1> This is the site template </p1>
{% endblock content %}
However, to describe a page content and not the site itself, I have the following page.html which is meant to inherit the code from base.html
{% extends “base.html” %}
{% block content %}
<h1>Greetings</h1>
<p>The_page_template</p>
{% endblock content %}
Changing views.py from render base.html to render page.html as follows causes the error.
from django.shortcuts import render
def index(request):
#return render(request, "base.html") // this works
return render(request, 'pages/page.html') // this does not
The uploaded image is a screenshot of the broken webpage. The best explanation I can offer is that there is an error in the spacing of the code or the pathing to page.html. With pathing I do notice the broken webpage does point to page.html. The following is a relevant statement from settings.py
‘DIRS’: [BASE_DIR / ‘site/templates’]
'APP_DIRS' : TRUE
For the sake of completeness, the following paths are described.
root/site/templates/base.html
root/pages/templates/pages/page.html
root/pages/views.py
root/site/settings.py
Any help appreciated in resolving this error - thanks

Django, following a tutorial and there's an issue where my {% block content %} is not being recognized

Here's the dir structure:
directory structure
If the image isn't displaying here's a text version:
templates
myapp
new_search.html
base.html
base.html code:
{% block content %}
{% endblock content %}
new_search.html code:
{% extends "base.html" %}
{% block content %}
<h2>NEW SEARCH</h2>
{% endblock %}
I can display the base.html fine.
The new_search.html however displays like this:
new_search.html
I also want to mention a side question. My Django server isn't running however I can still open the html in my browser. Is that supposed to happen? My URLs when opening both html's are:
base.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/base.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
new_search.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/myapp/new_search.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
Also, i'm getting this error when trying to visit my home page:
error screenshot
EDIT:
Adding more info. I now get this error. And my new_search template still isn't including the base template. I tried moving the html to different directories etc. In the tutorial it matches too.
template Doesn't Exist
it says the source doesn't exist but I can see it right there. proof
You might want to try copying "base.html" in templates --> myapp folder.
Basically maintain both hmtl files in the same directory.
And also try giving full path after copying them in same dir, like:
{% extends "myapp/base.html" %}
For the home page, you need to have the tag {% load static %} to be able to use the static tag.
Make sure you are using the render() function in your view so it doesn't just return the template as text
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

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

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

Categories