无法在Django视图中显示QuerySet

时间:2019-06-30 10:19:05

标签: python django

我正在尝试使用以下方法显示QuerySet, Passing Django Queryset in Views to Template

我能够使用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"

views.py

def index_view(request):
    rest_list = Restaurant.objects.all()
    context = {
        'rest_list': rest_list
    }
    return render(request, 'index.html', context)

index.html

    <h1>Index</h1>
    {% for rest in rest_List %}
        {{ rest.restId }}
        {{ rest.restName }}
    {%  endfor %}

1 个答案:

答案 0 :(得分:1)

将“ index.html”代码替换为以下代码:您的错误是“ rest_list”中的语法错误。

<h1>Index</h1>
{% for rest in rest_list %}
    {{ rest.restId }}
    {{ rest.restName }}
{%  endfor %}
  

Python是区分大小写的语言。