gpt4 book ai didi

python - Django:如何在模板的 if 语句中使用变量?

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:09 25 4
gpt4 key购买 nike

我的 views.py 传递了一个名为“preSelect”的变量,其中包含一个整数值。

在模板中,我想在 If 语句中使用 Integer 来检查当前的 for 循环计数器是小于等于还是大于我的值。

{% 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 %}

然而,这会返回以下错误:

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 '{{'

最佳答案

在 Jinja 中,当您使用 {%%} 标记时,不要将变量放在 {{}} 中

示例代码

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

所以你的代码是

{% if forloop.counter <= preSelect %}
# Do Something
{% endif %}

关于python - Django:如何在模板的 if 语句中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com