07 – STOCK MANAGEMENT SYSTEM – LISTING ITEMS FROM THE DATABASE

Spread the love

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'),

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

28 Comments

  • 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.

      • 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!

      • 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)

  • 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?

      • 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’

  • 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

      • 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.

  • 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

  • Salem,

    Try to add in the begining of your file view.py
    from .models import Stock

    it is woks now for me.

Leave a Reply

Your email address will not be published. Required fields are marked *