我可以在{{}}中使用jinja2中的{{}}嵌套

时间:2018-03-10 06:36:55

标签: python jinja2

昨天我想在{{}}中使用nest {{..}}时遇到错误,变量data是一个列表。  “data.append({'month':month,'loan_type':data_type,'channel':data_channel})”。 data_typedata_channel是字典。在我的'.tmpl'文件中,我传输了两个变量:datatitle_datatitle_data也是一个列表,它代表data.data_type和{{的字段名称1}}。 在我的data.data_channel文件中,我希望遍历.tmpl以显示数据。

title_data

但它引发了一个错误:

{% for record in data %}
        <tr>
            <td rowspan="14">record.month</td>
            <td rowspan="7">loan_angel</td>
        </tr>
        <tr>
            <td rowspan="7">loan_flow</td>
        </tr>
        <tr>
            <td>loan_angel</td>
            {% for title in title_data %}
            <td>{{ record.loan_type.loan_merchants_angel.{{ title }} }}</td>
            {% endfor %}
        </tr> 
{% endfor %}

Traceback (most recent call last): File "/Users/chenzhiping/Library/Application Support/IntelliJIdea2017.2/python/helpers/pydev/pydevd.py", line 1599, in <module> globals = debugger.run(setup['file'], None, None, is_module) File "/Users/chenzhiping/Library/Application Support/IntelliJIdea2017.2/python/helpers/pydev/pydevd.py", line 1026, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/Users/chenzhiping/Library/Application Support/IntelliJIdea2017.2/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/chenzhiping/WoSai/bsm-report/report/loan/tasks.py", line 1064, in <module> stat_loan_funnel_model_data() File "/Users/chenzhiping/WoSai/bsm-report/report/loan/tasks.py", line 954, in stat_loan_funnel_model_data 'finish_apply_merchants', 'approve_merchants', 'finish_sign_merchants']) File "/Users/chenzhiping/WoSai/bsm-report/report/notify.py", line 62, in gen_mail template = env.get_template(self.template) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 830, in get_template return self._load_template(name, self.make_globals(globals)) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 804, in _load_template template = self.loader.load(self, name, globals) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/loaders.py", line 125, in load code = environment.compile(source, name, filename) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 591, in compile self.handle_exception(exc_info, source_hint=source_hint) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise raise value.with_traceback(tb) File "/Users/chenzhiping/WoSai/bsm-report/report/templates/loan_summary_v4.tmpl", line 37, in template <td>{{ record.loan_type.loan_merchants_angel.{{ title }} }}</td> File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 497, in _parse return Parser(self, source, name, encode_filename(filename)).parse() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 896, in parse result = nodes.Template(self.subparse(), lineno=1) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 878, in subparse rv = self.parse_statement() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 130, in parse_statement return getattr(self, 'parse_' + self.stream.current.value)() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 198, in parse_for body = self.parse_statements(('name:endfor', 'name:else')) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 165, in parse_statements result = self.subparse(end_tokens) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 878, in subparse rv = self.parse_statement() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 130, in parse_statement return getattr(self, 'parse_' + self.stream.current.value)() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 198, in parse_for body = self.parse_statements(('name:endfor', 'name:else')) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 165, in parse_statements result = self.subparse(end_tokens) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 870, in subparse add_data(self.parse_tuple(with_condexpr=True)) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 615, in parse_tuple args.append(parse()) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 427, in parse_expression return self.parse_condexpr() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 432, in parse_condexpr expr1 = self.parse_or() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 445, in parse_or left = self.parse_and() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 454, in parse_and left = self.parse_not() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 465, in parse_not return self.parse_compare() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 469, in parse_compare expr = self.parse_math1() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 491, in parse_math1 left = self.parse_concat() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 502, in parse_concat args = [self.parse_math2()] File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 512, in parse_math2 left = self.parse_pow() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 523, in parse_pow left = self.parse_unary() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 542, in parse_unary node = self.parse_postfix(node) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 667, in parse_postfix node = self.parse_subscript(node) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 700, in parse_subscript self.fail('expected name or number', attr_token.lineno) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/parser.py", line 59, in fail raise exc(msg, lineno, self.name, self.filename) jinja2.exceptions.TemplateSyntaxError: expected name or number

data

{'month': month, 'loan_type': data_type, 'channel': data_channel}).

data_type

然后{'loan_merchants_angel': loan_merchants_angel, 'loan_merchants_flow': loan_merchants_flow}

loan_merchants_angel

0 个答案:

没有答案