If you are using this snippet: https://djangosnippets.org/snippets/2105/ you probably have a typo in your template. Try changing it to {% add_get_parameter %}
(not {% add_get_paramater %}
).
error using custom template tags in django? [closed]
-
21-07-2023 - |
题
I want to use pass parameters to the URL as get request within the template. Currently I am hard coding the url such as
<li><a href="{{ request.path|add:'?sort_by=vehicle_price' }}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
I found this code which seems to be elegant. so I tried to use it. but it throws error:
I have correctly placed the template tag inside myapp/templatetags/add_url_parameter.py
.
I have also registered django.core.context_processors.request'
in TEMPLATE_PROCESSORS
In the template, I have:
{% extends "base.html" %}
{% load bootstrap3 %}
{% load crispy_forms_tags %}
{% load add_url_parameter %}
{% block content %}
<div class="sortByLinks">
<h4>Sort by:</h4>
<ul class="list-inline">
<li>
{% if sort_by == "last_updated" %}
<li> <strong>New Post</strong>
<li><a href="{% add_get_paramater param1='vehicle_price' %}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
<li><a href="{% add_get_paramater param1='vehicle_year' %}" style="text-decoration: underline; color: red;">Latest Model Year</a></li>
{% elif sort_by == "vehicle_price" %}
<li><a href="{% add_get_paramater param1='last_updated' %}" style="text-decoration: underline; color: red;">New Post</a></li>
<li> <strong>Lowest Price</strong>
<li><a href="{% add_get_paramater param1='vehicle_year' %}" style="text-decoration: underline; color: red;">Latest Model Year</a></li>
{% else %}
<li><a href="{% add_get_paramater param1='last_updated' %}" style="text-decoration: underline; color: red;">New Post</a></li>
<li><a href="{% add_get_paramater param1='vehicle_price' %}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
<li> <strong>Latest Model Year</strong>
{% endif %}
</ul>
</div>
{% endblock %}
I get the following error:
Invalid block tag: 'add_get_paramater', expected 'elif', 'else' or 'endif'
In template /Users/Documents/python/django/my_app/templates/my_app/blog_detail.html, error at line 127
and line 127 is
<li><a href="{% add_get_paramater param1='vehicle_price' %}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
解决方案
不隶属于 StackOverflow