Django template Tag not showing

MK87

I am confused about how Django template tag can be shown.

I am using Django 2.0.3 and jQuery 3.3.1

I have this on a template called home.html:

//home.html
<script>
$(".game-menu").click(function () {
    $(".game-menu").removeClass("active");
    $(this).addClass("active")
});
$("#buildings").click(function () {
    $("#main-content").load("{% url 'game:buildings' %}");
    });
$("#overview").click(function () {
    $("#main-content").load("{% url 'game:overview' %}");
    });
</script>

<nav class="sidebar">
    <ul class="nav nav-pills flex-column">
        <li class="btn game-menu active" id="overview">
            <a class="nav-link text-white">Übersicht</a>
        </li>
        <li class="btn game-menu" id="buildings">
            <a class="nav-link text-white">Gebäude</a>
        </li>
    </ul>
</nav>

<!-- Page Content Holder -->
<main class="text-white" id="main-content">
    {% include 'game/overview.html' %}
</main>

including overview.html works fine as expected ( the template tag {{ planet.name }} showing on the page:

//overview.html
{% load static %}

<div class="card">
    <h2 class="card-header bg-dark">Übersicht - {{ planet.name }}</h2>
    <div class="card-body">
        <h5 class="card-title">Special title treatment</h5>
        <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
    </div>
</div>

my

views.py:

from django.template import loader
from django.http import HttpResponse

def overview_view(request):
template = loader.get_template('game/overview.html')
context = {}
return HttpResponse(template.render(context, request))

now the part that confuses me

if I click on the nav button overview to load the overview.html via jQuery into <main id=main-content></main> the template was shown, but the template tag {{ planet.name }} will not be shown.

Alasdair

You need to include planet in the context, for example:

def overview_view(request):
    template = loader.get_template('game/overview.html')
    context = {'planet': Planet.objects.get(name='Jupiter')}
    return HttpResponse(template.render(context, request))

Note you can use the render shortcut to simplify the view.

from django.shortcuts import render

def overview_view(request):
    context = {'planet': Planet.objects.get(name='Jupiter')}
    return render(request, 'game/overview.html', context)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related