我想在模板中使用传递参数作为get请求。目前我很难对网址进行编码,例如
<li><a href="{{ request.path|add:'?sort_by=vehicle_price' }}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
我发现this code似乎很优雅。所以我试着用它。但它会引发错误:
我已正确地将模板标记放在myapp/templatetags/add_url_parameter.py
内。
我还在django.core.context_processors.request'
TEMPLATE_PROCESSORS
在模板中,我有:
{% 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 %}
我收到以下错误:
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
和第127行是
<li><a href="{% add_get_paramater param1='vehicle_price' %}" style="text-decoration: underline; color: red;">Lowest Price</a></li>
答案 0 :(得分:1)
如果您使用的是此代码段:https://djangosnippets.org/snippets/2105/,您的模板中可能会出现拼写错误。尝试将其更改为{% add_get_parameter %}
(不是{% add_get_paramater %}
)。