【发布时间】:2020-08-01 22:30:04
【问题描述】:
我将 Views.py 中的列表以字典值的形式传递给 HTML。我在田野里循环。有一列是另一个模型的外键。只有该模型相关信息未在 HTML 中显示。如何解决这个问题?以下是代码。外键列“课程”未显示,但其他。 Screenshot attached here
Views.py
def Student_Main(request):
objs= Students.objects.values().all()
template_name = 'genius/students.html'
context = {'head_title': 'Little Genius Students', 'students':objs}
return render(request, template_name, context)
HTML
<table class="table table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">DOB</th>
<th scope="col">Age</th>
<th scope="col">Gender</th>
<th scope="col">Grade</th>
<th scope="col">School</th>
<th scope="col">Course</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
{% for i in students %}
<tr>
<th scope="row">{{i.id}}</th>
<td><a href=''>{{i.name}}</a></td>
<td>{{i.dob}}</td>
<td>{{i.age}}</td>
<td>{{i.gender}}</td>
<td>{{i.grade}}</td>
<td>{{i.attending_school}}</td>
<td>{{i.course.class_name}}</td>
<td>{{i.address}}</td>
</tr>
{% endfor %}
</tbody>
</table>
【问题讨论】:
标签: python html django python-3.x