Inherit template in Django

272 Views Asked by At

I am able to render the home.html and it prints Mary had a little lamb which is sitetitle

Here is the code for home.html

<head>
</head>
<body>
<h1>{{sitetitle}}</h1>
<h1>{% block fun %} {% endblock %}</h1>
</body> 

But it is not rendering the fun block in title.html, although it is in the same directory.

Here is the code for title.html

{% extends "home.html" %}
{% block fun %}
{{ link }}
{% endblock %}

Here is the code for views.py from django.shortcuts import render from models import siteprop from django.template import loader

def testf(request) :
 obj=siteprop.objects.first()
 context=obj.__dict__
 if '_state' in context: 
 del context['_state']
 print context
 return render(request,"home.html",context)

Here is the code for urls.py

from django.conf.urls import include, url
from django.contrib import admin
from thingslab import views
urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^test/',views.testf,),
]
1

There are 1 best solutions below

0
On

The fun block in title.html is not rendering because in your view you're rendering home.html.

Django does not automatically know that the code of title.html should be included in home.html just because it also contains a fun block.

You have to render title.html instead of home.html:

 return render(request, "title.html", context)