dmkanerer dmkanerer - 4 months ago 14
Python Question

Why wont my {% extends %} command work in my django app

I am making a web app with django, and in one portion I am trying to make use of the {% extends %} command to put some html from one template on to another. Here is the code:

home.html -

<!doctype html>
<html>
<head>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html


search.html -

{% extends "gamelobby/home.html" %}
{% block content %}
<h1>Hello World</h1>
{% endblock %}


Any idea what the problem might be?

Code for home.html view -

def index(request):
all_games = GameCard.objects.all()
template = loader.get_template('gamelobby/home.html')
context = {
'all_games': all_games,
}
return HttpResponse(template.render(context, request))

Answer

What you want to happen is to direct people to your search view so that view has to know about search.html

def index(request):
    all_games = GameCard.objects.all()
    template = loader.get_template('search.html') <!-- or whichever file -->
    context = {
        'all_games': all_games,
    }
    return HttpResponse(template.render(context, request))

When this view loads the template, it sees this extends from gamelobby/home.html and pulls it in around the block tags.