Reputation: 177
I'm checking a request.GET parameter in Django template. I'm pasting a portion of it:
<dd>
<i class="fa fa-caret-right {% if request.GET.order %}{% ifequal request.GET.order 'price-asc' %}active{% endifequal %}{% endif %}"></i> <a href="{%url_add_replace request 'order' 'price-asc'%}">Order by price (Asc)</a>
</dd>
As you see there is also a custom template tag named add_replace. It's basically adding the specified GET parameter to url. I don't think it makes problem.
My question is about something else. This code generates log at DEBUG level. And i'm trying to get rid of it. The log is below. I think there must be sth more suitable to check if a get parameter does exists or not. I can do it in views like:
get_dict = request.GET.copy()
if get_dict.__contains__('order'):
get_order = get_dict.__getitem__('order')
else:
get_order = None
But when i check it in the template, below log occurs:
DEBUG 2016-07-08 22:07:43,789 base 29571 140656761874496 Exception while resolving variable 'order' in template 'category.html'. Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/utils/datastructures.py", line 83, in __getitem__
list_ = super(MultiValueDict, self).__getitem__(key) KeyError: 'order'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 883, in _resolve_lookup
current = current[bit] File "/usr/local/lib/python3.5/site-packages/django/utils/datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key)) django.utils.datastructures.MultiValueDictKeyError: "'order'"
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 891, in _resolve_lookup
current = getattr(current, bit) AttributeError: 'QueryDict' object has no attribute 'order'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 898, in _resolve_lookup
current = current[int(bit)] ValueError: invalid literal for int() with base 10: 'order'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/template/base.py", line 905, in _resolve_lookup
(bit, current)) # missing attribute django.template.base.VariableDoesNotExist: Failed lookup for key [order] in '<QueryDict: {}>'
Any ideas?
UPDATE: I'm adding the custom template tag code:
@register.simple_tag(name='url_add_replace')
def url_add_replace(request, field, value):
dict = request.GET.copy()
dict.__setitem__(field, value)
return u"?%s" % (dict.urlencode())
Upvotes: 4
Views: 7864
Reputation: 31434
I think a custom template tag is overkill for this. The following template logic should work without triggering any debug logs:
{% if 'order' in request.GET %}
{% ifequal request.GET.order 'price-asc' %}active{% endifequal %}
{% endif %}
The difference between this and your original code is that the outer if
block is checking for the existence of order
in GET
, rather than evaluating the truthiness of GET.order
.
Upvotes: 11
Reputation: 177
I solved my problem with writing another custom tag:
@register.simple_tag(name='active_request_get')
def active_request_get(request, key, value):
dict = request.GET.copy()
if dict.__contains__(key):
if dict.get(key, default=None) == value:
return 'active'
return ''
I replaced this:
<dd>
<i class="fa fa-caret-right {% if request.GET.order %}{% ifequal request.GET.order 'price-asc' %}active{% endifequal %}{% endif %}"></i> <a href="{%url_add_replace request 'order' 'price-asc'%}">Order by price (Asc)</a>
</dd>
with this:
<dd>
<i class="fa fa-caret-right {% active_request_get request 'order' 'price-asc' %}"></i> <a href="{%url_add_replace request 'order' 'price-asc'%}">Order by price (Asc)</a>
</dd>
So i check the GET parameters as i want.
Upvotes: 0