我试图查询原始数据库,到目前为止在django shell中运行良好,但我无法列出模板中的所有记录。在模板中只显示最后一条记录。
这是我在views.py中的功能
def platformslist(request):
for platform in Platforms.objects.db_manager('nocdb').raw(
'SELECT * FROM lu_platform'):
platform
print(platform.name) # this is only for verified if working correctly in console
return render_to_response('platforms.html', {'platform': platform},
context_instance=RequestContext(request))
模板:
<div class="col-md-13">
<tr>
{{ platform.name }}
{{ platform.prefix }}
</tr>
和我的模特:
from django.db import models
from django.db.models.query import RawQuerySet
class Platforms(models.Model):
name = models.CharField()
prefix = models.CharField()
答案 0 :(得分:0)
您不仅要将最后一个平台传递给模板,还要传递所有平台:
def platformslist(request):
platforms = Platforms.objects.db_manager('nocdb').raw(
'SELECT * FROM lu_platform')
return render_to_response('platforms.html', {'platforms': platforms},
context_instance=RequestContext(request))
然后你可以遍历模板中的平台:
{% for platform in platforms %}
{{ platform.name }}
{% endfor %}