so im new in django and im trying to make a small market. i made a product app. this is the inside codes: this is for views.py:
from django.shortcuts import render
from django.http import HttpResponse
from products.models import product
def index(request):
Products = product.objects.all()
return render(request, 'index.html', {'products': Products})
def new(request):
return HttpResponse('New Product')
this is for models.py:
from django.db import models
class product(models.Model):
name = models.CharField(max_length=150)
price = models.FloatField()
stock = models.IntegerField()
image_url = models.CharField(max_length=2083)
i also made a template folder and put this in it for experiment:
<h1>Products</h1>
<ul>
{% for product in Products %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
and some other usual codes. but i get a pylint error for this part:
product.objects.all()
please help me! thanks
return render(request, 'index.html', {'products': Products}), it should be{% for product in products %}in the template. In Python, you usually useclass Product(models.Model):, and thenproducts = Product.objects.all(). If you do this, your code will be more readable for other Python programmers.