python - 无法在 Django 视图中显示 QuerySet
问题描述
我试图使用下面的方法显示一个 查询集,将视图中的 Django 查询集传递给模板
我可以使用 get() 方法显示单个对象,但是当我尝试返回餐厅表中的所有数据时,它会以空白页的形式返回。
一流餐厅
class Restaurant(models.Model):
restId = models.AutoField(db_column='restId', primary_key=True)
restName = models.TextField(db_column='restName')
phone = models.IntegerField()
address = models.TextField()
ratings = models.DecimalField(max_digits=2, decimal_places=1)
cuisine = models.TextField()
region = models.TextField()
#image = models.ImageField()
last_modify_date = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
managed = True
db_table = "restaurant"
视图.py
def index_view(request):
rest_list = Restaurant.objects.all()
context = {
'rest_list': rest_list
}
return render(request, 'index.html', context)
索引.html
<h1>Index</h1>
{% for rest in rest_List %}
{{ rest.restId }}
{{ rest.restName }}
{% endfor %}
解决方案
将“index.html”代码替换为以下代码:您的错误是“rest_list”中的语法错误。
<h1>Index</h1>
{% for rest in rest_list %}
{{ rest.restId }}
{{ rest.restName }}
{% endfor %}
Python 是一种区分大小写的语言。
推荐阅读
- python - Django:访问多对多中的额外字段(通过=)
- angular - 如何摆脱 WebStorm 中的 Angular 模板空间亮点
- c - 在 C 函数中拆分一个 int 数组并返回 2 个不同的数组
- php - 如何使用 laravel 在更新时从公用文件夹中删除旧图像
- selenium - github 在每个 selenium-webdriver 测试中要求验证码
- bash - 具有浮点值的变量的减法不起作用
- postgresql - 在定期更改数据库密码时实现连接重建机制
- c# - 如何从 HttpResponseMessage 中检索 Cookie
- delphi - 如何使用“for/in”在另一个组件中查找组件?
- scala - 如何使用设计模式或 OOP 编写 scala spark 代码以连接到多个云平台(Azure、AWS)