在 Django 模板中连接
concatenate in django template
为什么此代码段中的 id="{{field_index}}"
是空的,它不会根据键向我打印“field_version_”或“field_controparte”?
<form id="formDoc" style="margin-top: 10px;" action="/" method="post">
{% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
{% with field_index="field_"|add:keyFi|stringformat:"s" %}
<div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper"
根据add
模板过滤器的documentation:
This filter will first try to coerce both values to integers. If this
fails, it’ll attempt to add the values together anyway. This will work
on some data types (strings, list, etc.) and fail on others. If it
fails, the result will be an empty string.
我猜测您的 keyFi
变量是 None
或其他类型。尝试调试并检查该变量的值。
我也试过构建自定义标签
文件pyconcat_tags.py
from django import template
register = template.Library()
@register.filter
def concat_string(value_1, value_2):
return str(value_1) + str(value_2)
文件home.html
{% load pyconcat_tags %}
<form id="formDoc" style="margin-top: 10px;" action="process2_module_doc.php" method="post">
{% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
{% with field_index="field_"|concat_string:keyFi %}
<div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper">
输出我field_0,field_1取决于keyFi={"0":"version","1":"controparte"}
有效。
为什么此代码段中的 id="{{field_index}}"
是空的,它不会根据键向我打印“field_version_”或“field_controparte”?
<form id="formDoc" style="margin-top: 10px;" action="/" method="post">
{% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
{% with field_index="field_"|add:keyFi|stringformat:"s" %}
<div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper"
根据add
模板过滤器的documentation:
This filter will first try to coerce both values to integers. If this fails, it’ll attempt to add the values together anyway. This will work on some data types (strings, list, etc.) and fail on others. If it fails, the result will be an empty string.
我猜测您的 keyFi
变量是 None
或其他类型。尝试调试并检查该变量的值。
我也试过构建自定义标签
文件pyconcat_tags.py
from django import template
register = template.Library()
@register.filter
def concat_string(value_1, value_2):
return str(value_1) + str(value_2)
文件home.html
{% load pyconcat_tags %}
<form id="formDoc" style="margin-top: 10px;" action="process2_module_doc.php" method="post">
{% for keyFi,valueFi in tmplVar.jsonKeysDocFields.items %}
{% with field_index="field_"|concat_string:keyFi %}
<div style="margin-bottom: 5px; display: none;" id="{{field_index}}" class="docFieldWrapper">
输出我field_0,field_1取决于keyFi={"0":"version","1":"controparte"}
有效。