1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #13058 - "smart if" template tag doesn't support "if not in ..." condition

Thanks to ramusus for the report.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12732 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant
2010-03-08 23:25:37 +00:00
parent 021ba30ad1
commit 960af90279
6 changed files with 38 additions and 7 deletions

View File

@@ -806,8 +806,8 @@ def do_if(parser, token):
Arguments and operators _must_ have a space between them, so
``{% if 1>2 %}`` is not a valid if tag.
All supported operators are: ``or``, ``and``, ``in``, ``==`` (or ``=``),
``!=``, ``>``, ``>=``, ``<`` and ``<=``.
All supported operators are: ``or``, ``and``, ``in``, ``not in``
``==`` (or ``=``), ``!=``, ``>``, ``>=``, ``<`` and ``<=``.
Operator precedence follows Python.
"""

View File

@@ -97,6 +97,7 @@ OPERATORS = {
'and': infix(7, lambda x, y: x and y),
'not': prefix(8, operator.not_),
'in': infix(9, lambda x, y: x in y),
'not in': infix(9, lambda x, y: x not in y),
'=': infix(10, operator.eq),
'==': infix(10, operator.eq),
'!=': infix(10, operator.ne),
@@ -150,11 +151,23 @@ class IfParser(object):
error_class = ValueError
def __init__(self, tokens):
self.tokens = map(self.translate_tokens, tokens)
# pre-pass necessary to turn 'not','in' into single token
l = len(tokens)
mapped_tokens = []
i = 0
while i < l:
token = tokens[i]
if token == "not" and i + 1 < l and tokens[i+1] == "in":
token = "not in"
i += 1 # skip 'in'
mapped_tokens.append(self.translate_token(token))
i += 1
self.tokens = mapped_tokens
self.pos = 0
self.current_token = self.next()
def translate_tokens(self, token):
def translate_token(self, token):
try:
op = OPERATORS[token]
except (KeyError, TypeError):