为什么我的 jinja2 和 html 有显示问题?
Why am I having display problems with jinja2 and html?
我遇到了显示问题。我有一张正在填写数据的图表。除了当我每行有多个数据而不是其他数据进入该行时,它们会留在同一行。例如,当我检索用户提及并且结果有 3 screen_names 时,我的输出是:a b c
除了 a b c 从预期的列中突出并溢出到以下列中。我希望 a、b、c 在同一列中:
一个
\n
c\n
我的代码:
{% for item in res%}
<tbody>
<tr>
<th scope="row">{{ item['_source']['lang'] }}</th>
<td>{{ item['_source']['id'] }}</td>
<td>{{ item['_source']['date'] }}</td>
<td>{{ item['_source']['full_text']}}</td>
<td>{{ item['_source']['user']['name']}}</td>
<td>{{ item['_source']['user']['screen_name']}} </td>
<td>{{ item['_source']['user']['location']}} </td>
<td>{{ item['_source']['user']['id']}} </td>
{% for i in item['_source']['entities']['user_mentions'] if i["screen_name"] %}
<td> {{ i["screen_name"] }} <td>
{% else %}
<td> None <td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
您的 HTML 有多个问题。您没有关闭标签,并且循环中间有一个随机的 {% else %} 。根据我对您的问题的理解,您需要将其格式化为:
{% for item in res %}
<tbody>
<tr>
<th scope="row">{{ item['_source']['lang'] }}</th>
<td>{{ item['_source']['id'] }}</td>
<td>{{ item['_source']['date'] }}</td>
<td>{{ item['_source']['full_text'] }}</td>
<td>{{ item['_source']['user']['name'] }}</td>
<td>{{ item['_source']['user']['screen_name'] }} </td>
<td>{{ item['_source']['user']['location'] }} </td>
<td>{{ item['_source']['user']['id'] }} </td>
<td>
{% for i in item['_source']['entities']['user_mentions'] if i["screen_name"] %}
{{ i["screen_name"] }}<br>
{% endfor %}
</td>
</tr>
</tbody>
{% endfor %}
{% for item in res%}
<tbody>
<tr>
<th scope="row">{{ item['_source']['lang'] }}</th>
<td>{{ item['_source']['id'] }}</td>
<td>{{ item['_source']['date'] }}</td>
<td>{{ item['_source']['full_text']}}</td>
<td>{{ item['_source']['user']['name']}}</td>
<td>{{ item['_source']['user']['screen_name']}} </td>
<td>{{ item['_source']['user']['location']}} </td>
<td>{{ item['_source']['user']['id']}} </td>
{% for i in item['_source']['entities']['user_mentions'] if i["screen_name"] %}
<td> {{ i["screen_name"] }} <td>
{% else %}
<td> None <td>
{% endfor %}
</tr>
</tbody>
{% endfor %}
您的 HTML 有多个问题。您没有关闭标签,并且循环中间有一个随机的 {% else %} 。根据我对您的问题的理解,您需要将其格式化为:
{% for item in res %}
<tbody>
<tr>
<th scope="row">{{ item['_source']['lang'] }}</th>
<td>{{ item['_source']['id'] }}</td>
<td>{{ item['_source']['date'] }}</td>
<td>{{ item['_source']['full_text'] }}</td>
<td>{{ item['_source']['user']['name'] }}</td>
<td>{{ item['_source']['user']['screen_name'] }} </td>
<td>{{ item['_source']['user']['location'] }} </td>
<td>{{ item['_source']['user']['id'] }} </td>
<td>
{% for i in item['_source']['entities']['user_mentions'] if i["screen_name"] %}
{{ i["screen_name"] }}<br>
{% endfor %}
</td>
</tr>
</tbody>
{% endfor %}