簡體   English   中英

Django:如何在模板的if語句中使用變量?

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

我的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 %}

但是,這返回了以下錯誤:

環境:

請求方法:POST請求URL: http : //127.0.0.1 : 8000/

Django版本:1.10.2 Python版本:2.7.11已安裝的應用程序:['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django .contrib.messages','django.contrib.staticfiles','testsetcreation']已安裝中間件:['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.commonmon 。

模板錯誤:在模板D:\\ Django \\ testsetcreation \\ templates \\ testsetcreation \\ testsetView.html中,第61行的錯誤無法解析其余部分:'{{from from'{{'51:Comment 52:
軟件版本53:硬件版本
54:ABP 55:
項目56:57:
58:59:{%用於行中的行%} 60:61:{%forforloop.counter <= {{preSelect}}%} 62:
63:{%else%} 64:
65:{%endif%} 66:
{{row.1}} 67:{{row.2}} 68:69:
70:{{row.3}} 71:

追溯:

內部39中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ core \\ handlers \\ exception.py”。response = get_response(request)

_get_response 187中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ core \\ handlers \\ base.py”。response = self.process_exception_by_middleware(e,request)

_get_response 185中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ core \\ handlers \\ base.py”。response = wrapd_callback(request,* callback_args,** callback_kwargs)

testsetView 102中的文件“ D:\\ Django \\ testsetcreation \\ views.py”。返回渲染(請求,“ testsetcreation / testsetView.html”,上下文)

渲染30中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ shortcuts.py”。content = loader.render_to_string(模板名稱,上下文,請求,using = using)

render_to_string 67中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ loader.py”。template = get_template(template_name,using = using)

get_template 21中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ loader.py”。return engine.get_template(template_name)

get_template 39中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ backends \\ django.py”。return Template(self.engine.get_template(template_name),self)

get_template 160中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ engine.py”。template,origin = self.find_template(template_name)

find_template 134中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ engine.py”。name,template_dirs = dirs,skip = skip,

get_template 44中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ loaders \\ base.py”。內容,來源,origin.template_name,self.engine,

init 191中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ base.py”。self.nodelist = self.compile_nodelist()

compile_nodelist 233中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ base.py”。return parser.parse()

解析518中的文件“ c:\\ Python27 \\ lib \\ site-packages \\ django \\ template \\ base.py”。引發self.error(token,e)

異常類型:/異常值處的TemplateSyntaxError:無法解析其余部分:“ {{”中的“ {{”

在Jinja中,當您使用{%%}標記時,請勿將變量放在{{}}內

范例程式碼

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

所以您的情況下的代碼將是

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

為了直接回答您的問題,在默認的Django模板引擎中,當在條件語句中使用模板變量時,不必將它們用大括號括起來。 因此,只需刪除括號:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM