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,})