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
Try with this Use pylint --generated-members=objects
Install Django pylint:
pip install pylint-django
ctrl+shift+p > Preferences: Configure Language Specific Settings > Python
The settings.json available for python language should look like the below:
{
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],
"[python]": {
}
}
That is because PyLint
doesn't know anything about Django metaclasses which provide objects
attribute. Anyway your E1101 error is just a warning and you can disable it or use special pylint-django plugin to make PyLint aware of magic that Django does.
Another problem of your code is an incorrect usage of context passed to the render call:
return render(request, 'index.html', {'products': Products})
Context is a python dictionary object in which value
will be accessible in the template via key
. You are passing your queryset via products
key, but iterate over Products
(mind the first capital letter) key in your template which is not set so your template will not render anything.
enter code here
"python.linting.pylintArgs": [
"--load-plugins=pylint_django",
"--errors-only"
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With