1. Create a html page and save it as list_item.html
<!DOCTYPE html>
<html>
<head>
<title>Item List</title>
</head>
<body>
{{title}}<br>
{% for instance in queryset %}
{{instance.item_name}}, {{instance.quantity}}<br>
{% endfor %}
</body>
</html>
2. Make a view for listing the items from the database
def list_item(request):
title = 'List of Items'
queryset = Stock.objects.all()
context = {
"title": title,
"queryset": queryset,
}
return render(request, "list_item.html", context)
3. Add the URL
path('list_item/', views.list_item, name='list_item'),
queryset = Stock.objects.all()
this code is not working.
itis giving “attribute error ‘function object has no attribute objects’
sir please tell me its solution.
Let me see the content of your views.py file
queryset = Stock.objects.all()
this code is not working.
it is giving error “name ‘Stock’ is not defined’
please help me to understand what causing this error.
Thank you!
It is probably not imported from the models
from django.shortcuts import render
# Create your views here.
from stockmgmgt.models import Stock
def home(request):
title = ‘Welcome: This is the Home Page’
form = ‘This is the Home Page’
context = {
“title”: title,
“test”: form,
}
return render(request, “home.html”,context)
def list_items(request):
title = ‘List of Items’
queryset= Stock.object.all()
context = {
“title”: title,
“queryset”: queryset,
}
return render(request, “list_items.html”,context)
thanks
Salem,
Try to add in the begining of your file view.py
from .models import Stock
it is woks now for me.
hello ,when i type in urls.py : from stockmgmt import views, I have this error AttributeError: module ‘stockmgmt.views’ has no attribute ‘list_items’,can you help me please?
You will need to make a view named list_items in views.py.
I’ve got the same error .
my view.py file :
def home(request):
title = ‘Welcome: This is the Home Page’
form = ‘Welcome: This is the Home Page’
context = {
“title”: title,
“test”: form
}
return render(request, “home.html”,context)
def list_items(request):
title = ‘List of list_items’
queryset = Stock.objects.all()
context = {
“title”: title,
“queryset”: queryset,
}
return render(request, “list_items.html”,context)
can you help thanks .
Hi all,
The sample html page given while explaining in the video is written as ‘list_items’.
Be careful if pasting the codes from the site!
Because in the code here, it is written as ‘list_item’.
You will get an ‘AttributeError’
Thanks for catching that. It’s good to see you helping one another.
you missed the form .module import * in the video and Codes
Oh thanks. I will check that out
Hi there,
For me when i m trying to access “http://localhost:8000/list_items”, the page is loading constantly and showing nothing. No idea where the mistake is. Kindly advised what could be the reason.
Just to add more info, in power shell i m getting below error:
Internal Server Error: /list_items/
Traceback (most recent call last):
File “C:\Users\saadb\OneDrive\Study Material\Python\Inventory-System\venv\lib\site-packages\django\core\handlers\exception.py”, line 47, in inner
response = get_response(request)
File “C:\Users\saadb\OneDrive\Study Material\Python\Inventory-System\venv\lib\site-packages\django\core\handlers\base.py”, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\saadb\OneDrive\Study Material\Python\Inventory-System\venv\src\stockmgmt\views.py”, line 16, in list_items
queryset = Stock.objects.all()
NameError: name ‘Stock’ is not defined
Hi ,
I found out , now error has gone. I missed to add ‘from .models import * ‘ in views.py.
Thanks
You are doing good. Keep it up @Saad
It’s good to have errors. When you solve them, you become very happy. Expect another error very soon. Am sure will be able to solve them all gradually. It makes you a better developer because we are problem solvers. The more problems you solve, the better you become.
When this happen, you can try CTRL + Refresh. If that doesn’t work, you check codes again
File “C:\youtube\venv\src\djangoproject\urls.py”, line 22, in
path(‘list_items/’, views.list_items, name=’list_items’),
AttributeError: module ‘stockmgmt.views’ has no attribute ‘list_items’
urls.py
from django.contrib import admin
from django.urls import path
from stockmgmt import views
urlpatterns = [
path(”, views.home, name=’home’),
path(‘list_items/’, views.list_items, name=’list_items’), #line 22
path(‘admin/’, admin.site.urls)
]
pls my program is runing perfectly but not displaying (list of list_items
,) on its browser page! how can i disply the main items
are you using the correct variable names?
page loading successfully but not database contents are not loaded.
Finally done caught the error.Thank you
Nice job. Keep it up
Am glad you find and fixed the error
Salem,
Try to add in the begining of your file view.py
from .models import Stock
it is woks now for me.