Home > OS >  Failed template inheritance in django templating
Failed template inheritance in django templating

Time:09-16

I am in a middle of a project. The project uses basic html at the frontend. I having trouble in template inheritance. This is the basic code : -

    {% extends 'main.html' %}

{% block content %}
    <h2>Home</h4>
    <hr>
    {% if request.user.is_authenticated %}
        {% block home %}{% endblock home %}
    {% else %}

    {% for doc in doctor %}
        <div>
            <small>Doctors around</small>
            <br>
            <a href="{% url 'profile' doc.user.id %}"><li>{{doc.user.name}}</li></a>

            <br>
        </div>
    {% endfor %}

    {% endif %}

{% endblock content %}

Also the code is extended to another template. The child page is :-

    {% extends 'rec/home.html' %}
{% block home %}
    <div>
        {% if request.user.usertype == 'p' %}
            <h1>Hi {{request.user.name}} </h1>
        {% else %}
            <h1>Hi {{request.user.name}} </h1>
        {% endif %}
    </div>

{% endblock home %}

Both the files are in the same directory. But i have defined the templates dir in settings file in a different directory.

CodePudding user response:

It's just {% endblock %} you don't have to specify what block you're closing / I'm not sure you even can Wrong

That's the only issue I see with what's provided

CodePudding user response:

Instead of defining the template to extend from in each template, maybe try doing something like this:

Template.html

{% extends parent_template %}

Views.py

template = loader.get_template('app/page.html')
context = {}
context["parent_template"] = "app/parentPage.html"
return HttpResponse(template.render(context, request))

This way you can assign the template from the Django side and it should be easier to troubleshoot

  • Related