Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How can I use a variable inside an if statement in the template?

My views.py hands over a variable called "preSelect" that contains a Integer value.

Inside the template I want to use that Integer in an If statement to check if the current for loop counter is less equal or greater than than my value.

{% if forloop.counter <= {{ preSelect }} %}
    <td><input type="checkbox" name="checks" id="1" value={{ row.0 }} checked/></td>
{% else %}
   <td><input type="checkbox" name="checks" id="1" value={{ row.0 }} /></td>
{% endif %}

This however returns me the following error:

Environment:

Request Method: POST Request URL: http://127.0.0.1:8000/

Django Version: 1.10.2 Python Version: 2.7.11 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testsetcreation'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template error: In template D:\Django\testsetcreation\templates\testsetcreation\testsetView.html, error at line 61 Could not parse the remainder: '{{' from '{{' 51 : Comment 52 :
SW Version 53 : HW Version
54 : ABP 55 :
Project 56 : 57 :
58 : 59 : {% for row in rows %} 60 : 61 : {% if forloop.counter <= {{ preSelect }} %} 62 :
63 : {% else %} 64 :
65 : {% endif %} 66 :
{{ row.1 }} 67 : {{ row.2 }} 68 : 69 :
70 : {{ row.3 }} 71 :

Traceback:

File "c:\Python27\lib\site-packages\django\core\handlers\exception.py" in inner 39. response = get_response(request)

File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\Django\testsetcreation\views.py" in testsetView 102. return render(request, 'testsetcreation/testsetView.html', context)

File "c:\Python27\lib\site-packages\django\shortcuts.py" in render 30. content = loader.render_to_string(template_name, context, request, using=using)

File "c:\Python27\lib\site-packages\django\template\loader.py" in render_to_string 67. template = get_template(template_name, using=using)

File "c:\Python27\lib\site-packages\django\template\loader.py" in get_template 21. return engine.get_template(template_name)

File "c:\Python27\lib\site-packages\django\template\backends\django.py" in get_template 39. return Template(self.engine.get_template(template_name), self)

File "c:\Python27\lib\site-packages\django\template\engine.py" in get_template 160. template, origin = self.find_template(template_name)

File "c:\Python27\lib\site-packages\django\template\engine.py" in find_template 134. name, template_dirs=dirs, skip=skip,

File "c:\Python27\lib\site-packages\django\template\loaders\base.py" in get_template 44. contents, origin, origin.template_name, self.engine,

File "c:\Python27\lib\site-packages\django\template\base.py" in init 191. self.nodelist = self.compile_nodelist()

File "c:\Python27\lib\site-packages\django\template\base.py" in compile_nodelist 233. return parser.parse()

File "c:\Python27\lib\site-packages\django\template\base.py" in parse 518. raise self.error(token, e)

Exception Type: TemplateSyntaxError at / Exception Value: Could not parse the remainder: '{{' from '{{'

like image 702
r00flr00fl Avatar asked Nov 11 '16 11:11

r00flr00fl


People also ask

How do you pass variables from Django view to a template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

What does %% include?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.


1 Answers

in Jinja, when you use the {%%} marks, don't put the variables inside {{}}

Example Code

{% if x > y %}
    # Do something
{% endif %}

So the code in your case would be

{% if forloop.counter <= preSelect %}
    # Do Something
{% endif %}
like image 174
Michael Yousrie Avatar answered Sep 19 '22 00:09

Michael Yousrie