【发布时间】:2014-08-07 14:27:25
【问题描述】:
我查看了以前的问题,但找不到问题的答案。
我正在尝试显示一个 2D 列表,这是我之前在 HTML 中使用不同列表完成的。
我对另一个列表使用了类似的方法,但它不会显示,我只是得到标题。
HTML:
<table class="listtable" >
<thead>
<tr>
<th>No</th>
<th>Account No</th>
<th>Time</th>
<th>Message</th>
</tr>
</thead>
{% for person in user %}
<tr>
{% for message in person %}
<td>{% autoescape off %}{{ message }}{% endautoescape %}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
我已经在视图中打印了列表,所以我知道在视图中它是正确的。我 100% 确定我也使用了正确的变量。
视图中的代码类似于:
user = get_data_from_other_source()
for item in user:
print(item)
打印件准确地显示了它应该显示的内容。
该列表大致如下:
[4, '<account number>', '<time>', 'somestring']
[3, '<account number>', '<time>', 'somestring']
页面来源说:
<table class="listtable" >
<thead>
<tr>
<th>No</th>
<th>Account No</th>
<th>Time</th>
<th>Messages</th>
</tr>
</thead>
</table>
编辑:审核代码..
@login_required(login_url='/login')
def page_control(request):
acc_no = request.session['acc_no']
user = setup_page_control (acc_no)
for item in user:
print(item)
return render_to_response("<htmlfile>.html",
locals(),
context_instance=RequestContext(request))
def setup_user_control(acc_no):
messages = <outside magic>
user = reversed(messages)
return user
我从代码中删除了很多内容,并更改了变量名等。 我只删除了我 100% 确定不是问题的内容
【问题讨论】:
-
使用不同的上下文变量名。将
user更改为user_list之类的。 -
如果可以的话,粘贴整个视图代码。
-
我已经更改了变量名,“用户”是别的东西,为了安全我不得不更改一些东西
-
我想补充一点,我没有为此使用内置数据库:)
标签: django python-3.x django-templates