简体   繁体   中英

Displaying Django model on template

Nothing is being displayed. even without any error log. although the model has data in it

models.py

class Product(models.Model):
    name = models.TextField()
    cat = models.IntegerField()
    price = models.FloatField()
    company_id = models.IntegerField()
    status = models.TextField()

views.py

def list_products(request):
    object_list = Product.objects.all()
    return render(request, 'index.html', {'object_list ': object_list})

index.html

   {% for e in object_list %}
      <li> {{ e.name }} </li>
   {% endfor %}

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.list_products, name='index'),

]

Fix a typo in the render context:

{'object_list ': object_list}

make it

{'object_list': object_list}

You have and an extra " " whitespace after object_list key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM