Django 템플릿에서 request.GET 매개 변수를 확인하고 있습니다. 나는 그 일부를 붙이고 있습니다.Django 템플릿에서 요청 get 매개 변수를 확인하십시오.
<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>
여러분도 알다시피 add_replace라는 사용자 정의 템플릿 태그가 있습니다. 기본적으로 URL에 지정된 GET 매개 변수를 추가합니다. 나는 그것이 문제가된다고 생각하지 않는다.
내 질문에 대한 다른 질문이 있습니다. 이 코드는 DEBUG 수준에서 로그를 생성합니다. 그리고 나는 그것을 없애려고 노력하고있다. 로그는 아래와 같습니다. get 매개 변수가 존재하는지 여부를 확인하는 데 더 적합한 sth가 있어야한다고 생각합니다. 내가 좋아하는 뷰에서 작업을 수행 할 수 있습니다
get_dict = request.GET.copy()
if get_dict.__contains__('order'):
get_order = get_dict.__getitem__('order')
else:
get_order = None
하지만 난 로그 아래에 템플릿을 검사 할 때 발생합니다
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: {}>'
어떤 아이디어?
업데이트 :
@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())
감사합니다. 최대한 빨리 시도하고 결과를 공유하겠습니다. – Jai