Django template not detecting builtin tag

Hi, I am facing weird error while use Django Built in template tag.
I am creating partial part of templates and use context variable to determine wether the templates will rendered full by extending to base.html as parent or rendered without extending to base.html. The problem is this line in create.html

{% if not no_render %}
    {% extends 'base.html' %}
{% endif %}

This code return me an error said {% endif %} is not registered


It not just the {% endif %} but also {% else %} or {% elif %} will return same error. I don’t know whats wrong as other if statement works fine in other lines.

If I delete if statement and extending to base.html everything goes normal.

this work normal

{% extends 'base.html' %}

{% load customtags %}
{% load static %}
{% block content %}
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% for field in form %}
            <div class="mb-3">
                <label for="id_{{field.name}}" class="form-label">{{field.label}}</label>
                {{field|add_form_class:'form-control'}}
                {% if field.errors %}
                    {{field.errors}}
                {% endif %}
                <span class="form-text">{{field.help_text}}</span>
            </div>
        {% endfor %}
        <input type="submit" value="Save" class="btn btn-primary">
    </form>
    {% if request.resolver_match.view_name == 'shorturl:edit' %}
        <form method="post" action="{% url 'shorturl:delete' %}">
            {% csrf_token %}
            <input type="hidden" name="short" value="{{request.resolver_match.kwargs.url}}">
            <input type="hidden" name="domain" value="{{request.resolver_match.kwargs.domain}}">
            <input type="submit" value="Delete" class="btn btn-primary">
        </form>
    {% endif %}

the result should be:

This does not work

{% if not no_render %}
    {% extends 'base.html' %}
{% endif %}

{% load customtags %}
{% load static %}
{% block content %}
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {% for field in form %}
            <div class="mb-3">
                <label for="id_{{field.name}}" class="form-label">{{field.label}}</label>
                {{field|add_form_class:'form-control'}}
                {% if field.errors %}
                    {{field.errors}}
                {% endif %}
                <span class="form-text">{{field.help_text}}</span>
            </div>
        {% endfor %}
        <input type="submit" value="Save" class="btn btn-primary">
    </form>
    {% if request.resolver_match.view_name == 'shorturl:edit' %}
        <form method="post" action="{% url 'shorturl:delete' %}">
            {% csrf_token %}
            <input type="hidden" name="short" value="{{request.resolver_match.kwargs.url}}">
            <input type="hidden" name="domain" value="{{request.resolver_match.kwargs.domain}}">
            <input type="submit" value="Delete" class="btn btn-primary">
        </form>
    {% endif %}

This code below in my views.py

class CreateURLView(View):
    templates = 'short/create.html'
    title = 'URL Shortener | Shorten and Manage HyperLinks'
    qrapi = "http://api.qrserver.com/v1/create-qr-code/?data={}&size=300x300&format=png"

    def get(self, request, *args, **kwargs):
        form = ShortForm()
        if request.user.is_authenticated:
            form.fields['group'].queryset = GroupURL.objects.filter(user=request.user)
        else:
            form.fields.pop('group')

        if request.headers['content-type'] == 'application/json':
            response = render_to_string(self.templates, {'form': form, 'no_render':True}, request)
            data = json.dumps({'success': True, 'data': response, 'title': self.title})
            return JsonResponse(data)
        else:
            return render(request, self.templates, {'form': form, 'title': self.title,})

Post the full template code and highlight what if statements work and those who do not work.

I have updated my question and posted the code that works and the code that doesn’t.

I guess you need first the static tag and then make a condition. Try this to the part that does not work:

{% load customtags %}
{% load static %}


{% if not no_render %}
{% extends 'base.html' %}
{% endif %}

thanks for your help, already tried that before and the issues still persist. Django always tell me that {% endif %} is not registered

after few hours trying to fix this I came with a conclusion that {% extends %} can not wraped in “if” statement, as I try this line and it worked.

{% if not no_render %}
    <p>tes this one</p>
{% endif %}

Correct. This is documented at The Django template language | Django documentation | Django

If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.

1 Like

ah yeah know I remember this part in Documentation, This little thing was forgotten by me. is there any discussion or documentation how to eliminate {% extends %} in dynamic conditions? I’ve searched bout it but didn’t find anything helpful, I don’t want to write the entire part of my templates create.html.
Thanks

You have at least two options:

  • Using a package such as django-template-partials (That’s what all the young, cool kids such as Carlton and Adam, are doing.)

  • Separating out the part of the template to be returned as a fragment into a separate template file, and then using the include tag to include it in the full-page rendering when needed. (That’s what we’ve been doing.)