NordicFox
NordicFox

Reputation: 145

Unable to display QuerySet in Django view

I was trying to display a QuerySet using method below, Passing Django Queryset in Views to Template

I was able to display single object by using get() method, however it returns as a blank page when I try to return all data in the Restaurant Table.

Class Restaurant

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 %}

Upvotes: 1

Views: 335

Answers (1)

Milad Hatami
Milad Hatami

Reputation: 1650

Replace 'index.html' code with below code: Your mistake is a syntax error in 'rest_list'.

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

Python is a case sensitive language.

Upvotes: 1

Related Questions