Why the ListView not showing data in the template? - python

I'm working on my first Django project (the final project for codecademy's Django class) and I'm making webpages to show the inventory and menu that a restaurant has. I made the model, view, template, etc. for inventory and it displays the ListView perfectly. I did the same thing for my menu and it doesn't work. The page loads but the table that's supposed to output data is empty.
Any insight on what might be going wrong?
PS I'm new to programming and this is my first stackoverflow post so forgive any formatting errors or other faux pas
## views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import Inventory, MenuItem, RecipeRequirement, Purchase
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views.generic import ListView
# Create your views here.
def index(request):
return render(request, "index.html")
class InventoryList(ListView):
template_name = "inventory.html"
model = Inventory
class MenuList(ListView):
template_name = "menu.html"
model = MenuItem
Inventory (below) works fine! :)
{% extends './base.html' %}
{% block content %}
<h2>Inventory</h2>
<table id="inventory">
<tr>
<th>Ingredient</th>
<th>Price</th>
<th>Units Available</th>
</tr>
{% for ingredient in inventory_list %}
<tr>
<tr>
<td>{{ ingredient.ingredient_name }}</td>
<td>{{ ingredient.price }}</td>
<td>{{ ingredient.units_avail }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
This one (Menu) is the problematic one :(
{% extends './base.html' %}
{% block content %}
<h2>Menu</h2>
<table id="menu">
<tr>
<th>Item</th>
<th>Price</th>
<th>In Stock?</th>
</tr>
{% for food in menu_list %}
<tr>
<tr>
<td>{{ food.menu_item_name }}</td>
<td>{{ food.price }}</td>
<td>{{ food.available }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
Models below
from django.db import models
from django.forms import DateTimeField
# Create your models here.
class Inventory(models.Model):
ingredient_name = models.CharField(max_length=30)
price = models.DecimalField(max_digits=5, decimal_places=2)
units_avail = models.IntegerField()
def __str__(self):
return self.ingredient_name + " avail: " + str(self.units_avail)
class MenuItem(models.Model):
menu_item_name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return self.menu_item_name + " with Price: " + str(self.price)
def available(self):
return all(recipe_req.enough() for recipe_req in self.reciperequirement_set.all())
class RecipeRequirement(models.Model):
ingredient = models.ForeignKey(Inventory, on_delete=models.CASCADE)
menu_item = models.ForeignKey(MenuItem, on_delete=models.CASCADE)
quantity = models.IntegerField()
def __str__(self):
return self.ingredient.ingredient_name + " in " + self.menu_item.menu_item_name
def enough(self):
return self.quantity <= self.ingredient.units_avail
class Purchase(models.Model):
menu_item = models.ForeignKey(MenuItem, on_delete=models.CASCADE)
timestamp = models.DateTimeField()
def __str__(self):
return self.menu_item.menu_item_name + " at " + self.timestamp
URLs below
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("inventory", views.InventoryList.as_view(), name="inventory_list"),
path("menu", views.MenuList.as_view(), name="menu_list"),
]

I could see one problem, you have one extra <tr> tag open in both the templates just below the loop.
Also think some problem with available method you manually defined in MenuItem model, so I removed it, try without it.
Also I'd recommend you to use context_object_name which is by default object_list so:
views.py
class InventoryList(ListView):
template_name = "inventory.html"
model = Inventory
context_object_name="inventories"
class MenuList(ListView):
template_name = "menu.html"
model = MenuItem
context_object_name="menu_list"
inventory.html template
{% extends './base.html' %}
{% block content %}
<h2>Inventory</h2>
<table id="inventory">
<tr>
<th>Ingredient</th>
<th>Price</th>
<th>Units Available</th>
</tr>
{% for ingredient in inventories %}
<tr>
<td>{{ ingredient.ingredient_name }}</td>
<td>{{ ingredient.price }}</td>
<td>{{ ingredient.units_avail }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
menu.html template
{% extends './base.html' %}
{% block content %}
<h2>Menu</h2>
<table id="menu">
<tr>
<th>Item</th>
<th>Price</th>
<th>In Stock?</th>
</tr>
{% for food in menu_list %}
<tr>
<td>{{ food.menu_item_name }}</td>
<td>{{ food.price }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
Note: It's recommended to give / at the end of every route so your urls.py should be:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("inventory/", views.InventoryList.as_view(), name="inventory_list"),
path("menu/", views.MenuList.as_view(), name="menu_list"),
]
Note: class based views in Django requires their name to be written as model name as prefix and actual view name as suffix, so you may change it to InventoryListView and MenuItemListView from InventoryList and MenuList respectively.

I notice that the RecipeRequirement model Class has the method available. This method calls the method enough from the RecipeRequirement model Class.
One troubleshooting strategy is to disable (comment out) the methods enough and available as they are introducing logic that would prevent display of menu items. In other words, your code appears to read: If we don't have enough ingredients there is no point returning menu items for recipes that we cannot make.
An alternative troubleshooting strategy is to populate the RecipeRequirement table with sufficient ingredients and it should work. Caveat: This answer is untested.
Here's a tip for a newcomer to coding that has served me well in my brief journey into software development. Errors have been my greatest teacher. When there are errors try to reduce as many elements as you can. When you hit a wall like this consider what is essential and comment out the rest. I suggested commenting out the two methods first because they are not required for the tables to display (or for what you report as being central to your question). Once you get the menu items displaying then you can re-introduce those methods one at time.

Related

Is there a way to isolate a foreign key from its related model using django?

I am building a django website that displays the prices of items in a shop. Each item belongs to a category so I mande category a foreign key that can have one or more items.
`class Category(models.Model):
category = models.CharField(max_length=64)
def __str__(self):
return self.category
class ShopItem(models.Model):
itemName = models.CharField(max_length=64)
price = models.IntegerField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return self.itemName`
However, in the html, I am unable to obtain both the category and the shop item
I tried the following...
`---snip---
{% used_category = [] %}
{% for item in shopitem_list %}
{% if item.category not in used_category %}
<tr>
<td>item.category</td>
<td></td>
<td></td>
</tr>
<tr>
{% for rel_item in shopitem_list %}
{% if rel_item.category == item.category %}
<td></td>
<td>rel_item.itemName</td>
<td>rel_item.category</td>
{% endif %}
{% enfor %}
</tr>
{% endif %}
{% endfor %}`
I was hoping this would help me create a table where all the items are sorted below their respective categories but I got an error instead:
TemplateSyntaxError at /
Invalid block tag on line 18: 'used_category', expected 'endblock'. Did you forget to register or load this tag?
You need to create a view function which will pass the required Models information to the Template, thus completing the Model - View - Template (MVT) method which django runs on.
You can't create and update a list within the template, but you can prepare the data in the view function, then iterate over your items differently.
Start by making sure the categories are in your template context. To avoid n+1 queries, prefetch the related items also. Let's say your view looks like this (updated):
from django.shortcuts import render
from .models import Category
def items_list(request):
context = {
"categories": Category.objects.exclude(shop_items=None).prefetch_related(
"shop_items"
)
}
return render(request, "my_template.html", context)
Then in the template iterate over the categories:
{% for category in categories %}
<tr>
<td colspan="3">{{ category.category }}</td>
</tr>
<tr>
{% for item in category.shop_items %}
<td></td>
<td>{{ item.itemName }}</td>
<td>{{ item.price }}</td>
{% enfor %}
</tr>
{% endfor %}
NB The related name shop_items may not work here. To make sure it does, you can manually set the related_name on your foreign key. At the same time I would suggest changing itemName to just name:
class ShopItem(models.Model):
name = models.CharField(max_length=64)
price = models.IntegerField()
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="shop_items")
def __str__(self):
return self.name

I have an issue with the changeStock method of a Product class in views.py in Django

I am doing a Product class in Django for fun and I have ran into a problem regarding changing the Product's (the model name) regarding its variable qtyOnHand, which is used to check to see how much of that product is in stock.
The goal is to change the product's qtyOnHand by a specified amount using the changeStock method and redirect to the home page where the updated qtyOnHand will be stored. The amount can be negative if taking out stock, and +ve if receiving stock. The error I got when typing in the URL
http://127.0.0.1:8000/products/1/changeStock/2 is unsupported operand types for +: int and str, which was
Here's my code for the changeStock method in the views.py. The comments there are to guide my approach to the problem.
# Change the stock of a specific product by the specified amount. A -ve number means that you are taking out product,
# while a +ve one means you are receiving that product.
def changeStock(request, pk, amount):
# We need the id of a specific product, plus we have to check if it's not found.
product = get_object_or_404(Product, pk=pk)
# Then we have to update its qtyOnHand.
product.qtyOnHand = product.qtyOnHand + amount -> error occurs at this line
# Then we have to save the changes to the database.
product.save()
# Then we have to redirect to the home page.
return render(request, 'home.html', {'product': product})
Here is my urls.py, which is used to store my URLs. The part (?P-?\d+) is used to include numbers that are positive and negative numbers.
from django.conf.urls import url
from django.contrib import admin
from inventories import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^products/(?P<pk>\d+)/$', views.product_topics, name='product_topics'),
url(r'^products/(?P<pk>\d+)/new/$', views.new_product, name='new_product'),
url(r'^products/(?P<pk>\d+)/changeStock/(?P<amount>-?\d+)$', views.changeStock, name='home'),
url(r'^admin/', admin.site.urls),
]
In my home.py, which shows all the products
{% extends 'base.html' %}
{% block breadcrumb %}
<li class="breadcrumb-item active">Products</li>
{% endblock %}
{% block content %}
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Products</th>
<th>Price</th>
<th>Quantity on Hand</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>
{{ product.name }}
<small class="text-muted d-block">{{ product.description }}</small>
</td>
<td class="align-middle">
{{ product.price }}
</td>
<td class="align-middle">{{ product.qtyOnHand }} </td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
And here's models.py for my Product class
from django.db import models
# Create your models here.
class Product(models.Model):
name = models.CharField(max_length=30)
description = models.TextField(max_length=4000)
price = models.DecimalField(max_digits=19,decimal_places=2)
qtyOnHand = models.IntegerField()
def __str__(self):
return self.name
Can someone show me what this error means, and how I can fix it?
You passed parameter is taken as string in view.so convert it to int. product.qtyOnHand is always int as defined in model.So not add string type to int.Please change product.qtyOnHand = product.qtyOnHand + amount to
product.qtyOnHand = product.qtyOnHand + int(amount)
I suggest like this.

Django accessing Model with property decorator

How do I access the data in the method with the property decorator. I can access it in the shell, and it is what I need, but it comes up blank in my site.
Models.py:
class Project(models.Model):
date_published = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
area = models.ForeignKey(Area, on_delete=models.PROTECT)
title = models.CharField(max_length=128, unique=True)
summary = models.CharField(max_length=256)
others = models.CharField(max_length=128, blank=True)
deadline = models.DateField(null=True, blank=True)
priority = models.ForeignKey(Priority, on_delete=models.PROTECT)
closed = models.DateTimeField(null=True, blank=True)
#property
def updates(self):
updates = []
categories = set(self.update_set.all().values_list(
'category__id', flat=True))
for cat_id in categories:
updates.append(Update.objects.filter(
project=self, category__id=cat_id).order_by('added').last())
return updates
def __str__(self):
return self.title
Views.py:
class ProjectView(ListView):
template_name = 'project_portal/home.html'
queryset = Project.objects.all()
And I am trying to use the following in my template:
<div class="box5">
{% for item in object_list %}
<table>
<tr>
<td>{{ item.updates }}</td>
</tr>
</table>
{% endfor %}
</div>
So far the box is blank. However, I have managed to get this data in the Django shell with the following:
p = Project.objects.all()[0]
p.updates
This returns the correct data in the right order for the first project. What do I need to do to get it to appear in the site?
It looks like your problem is that you are using .values() in your queryset in your view. This will give you a list. The reason why it works in the shell is because you are working with model objects, and hence you are able to get the updates property. So remove .values() from queryset = Project.objects.all().values()
Read up more about .values() here.
Are you sure the template has a var named project? Try changing project for object_list or override the get_context_data method to pass project to the template.
Edit: After reading your edited question I think you want to do something like this.
<div class="box5">
{% for project in object_list %}
<table>
<tr>
{% for category_update in project.updates %}
<td>{{ category_update }}</td>
{% endfor %}
</tr>
</table>
{% endfor %}
</div>
If you keep watching the category update in a wrong way please share with us your Category Model.
So this is now solved. The Models.py and Views.py in the original post are correct now, what was causing the rest of the issue was the Template, here is a short version of what was needed:
{% block home %}
{% if object_list %}
{% for project in project_list %}
<div class="box1">
<h4>{{ project.title }}</h4>
<p>Project Status</p>
<div class="square"></div>
</div>
<div class="box5">
{% for item in project.updates %}
<table>
<tr>
<td>{{ item }}</td>
</tr>
</table>
{% endfor %}
</div>
{% endfor %}
{% endif %}
The point I am making here is that there are two loops, one loops through projects in the object_list, and the second loop loops through items within the project.updates, which is fed by the method with the property decorator. I hope this is clear if anyone finds this looking for help.

Django View check second model for value

I'm somewhat new to Django so please bear with me here. I have a three models in my app, Company, Articles, and Transcripts. Both Articles and Transcripts have a foreignkey() field that links to the Company model. In my main view I list all of the companies, but I would like to indicate if there is an article or transcript for each of those companies.
Here are the models:
class Company(models.Model):
stock_symbol = models.CharField(max_length=5, unique=False)
company_name = models.CharField(max_length=200)
address = models.CharField(max_length=300, blank=True)
website = models.URLField(max_length=200, blank=True)
following = models.BooleanField()
class Articles(models.Model):
company = models.ForeignKey('Company', related_name='articles')
headline = models.CharField(max_length=200)
url = models.URLField(max_length=300, blank=True)
class Transcripts(models.Model):
company = models.ForeignKey('Company', related_name='transcripts')
headline = models.CharField(max_length=200)
url = models.URLField(max_length=300, blank=True)
Below is a view for a single company, however since I'm returning all companies I obviously can't use a primarykey. How would I go about getting this info for each of the companies? Am I setting up my models the right way to do this?
class CompanyDetails(generic.DetailView):
model = Company
template_name = 'company_details.html'
def get_context_data(self, **kwargs):
pk = self.kwargs.get('pk')
context = super(CompanyDetails, self).get_context_data(**kwargs)
context['company'] = Company.objects.filter(id=pk)
context['articles'] = Articles.objects.filter(company_id=pk).order_by('-date')
context['transcripts'] = Transcripts.objects.filter(company_id=pk).order_by('-date')
return context
UPDATE
How do I update the template if I want to show in a column that there is at least one article tied to each company? When I tried the for loop in the answer it created a column for every article, so I tried changing it to the below but it every records shows up as "Blank".
<tbody>
{% for company in all_companies %}
<tr>
<td nowrap="true">{{ company.company_name }}</td>
<td nowrap="true">{{ company.stock_symbol }}</td>
{% if company.following %}
<td align="center"><span class="glyphicon glyphicon-star"></span></td>
{% else %}
<td></td>
{% endif %}
<!--HAS ARTICLE(S) FLAG -->
{% if article in company.articles.all %}
<td align="center"><span class="glyphicon glyphicon-list-alt"></span></td>
{% else %}
<td>Blank</td>
{% endif %}
<td nowrap="true">{{ company.website }}</td>
<td nowrap="true">{{ company.address }}</td>
</tr>
{% endfor %}
</tbody>
In the template, you can follow the foreign key backwards from a company to its articles or transcripts.
{{ company }}
{% for company in company.articles.all %}
{{ article }}
{% endfor %}
If you use this in your detail view, you don't have to set articles or transcripts in the get_context_data method. You shouldn't set context['company'] at all - the detail view does this for you.
In the list view, you can loop through the companies and access the articles and transcripts in the same way.
{% for company in company_list %}
{{ company }}
{% for company in company.articles.all %}
{{ article }}
{% endfor %}
{% endfor %}
This will cause a query for every single company in the queryset. You can prevent this by using prefetch_related.
Company.objects.prefetch_related('articles', 'transcripts')
In your list view, you can use prefetch_related by setting queryset:
class CompanyList(generic.ListView):
# No need to set model now that you have set queryset
queryset = Company.objects.prefetch_related('articles', 'transcripts')
If you just want to know whether or not there are associated articles or transactions, then you don't have to prefetch them. You can annotate the queryset instead:
from django.db.models import Count
# in your view
queryset = Company.objects.annotate(num_articles=Count('article')
Then in your template, you can access company.num_articles.
{% if company.num_articles %}
Company has articles
{% endif %}

django parent child summation in view and pass to a template

I am new to Django and would like to do the following i have a simple project and have a parent and child model of a Project and related Project Tasks. One Project will have multiple ProjectTasks.
I want to be able to display the Project and ProjectTask sum through a template.
class Project(models.Model):
projectContact = models.ForeignKey(Contact)
projectTitle = models.CharField(max_length=100)
projectAddress1 = models.CharField(max_length=200)
projectValueProposed = models.IntegerField(default=0)
projectValueActual = models.IntegerField(default=0)
def __str__(self):
return self.projectTitle
class ProjectTask(models.Model):
projectName = models.ForeignKey(Project)
projectTaskDesc = models.CharField(max_length=200)
projectTaskContact = models.ForeignKey(Contact)
projectTaskAmount = models.IntegerField(default=0)
def __str__(self):
return self.projectTaskDesc
My view is as follows.
def index(request):
project_list = Project.objects.all()
project_task = ProjectTask.objects.all()
##project_task = ProjectTask.objects.all() ##filter(projectName=2).aggregate(amount=Sum('projectTaskAmount'))
for pid in project_list:
ptask = project_task.filter(projectName=pid.id)
context = {'project_list':project_list,'project_task':ptask}
return render(request,'construction/index.html',context)
and template is
{% if project_list %}
<table class="tg">
<tr>
<th class="tg-le8v">Project name</th>
<th class="tg-le8v">Customer</th>
<th class="tg-le8v">Proposed Value</th>
<th class="tg-le8v">Actual Value</th>
</tr>
{% for projects in project_list %}
<tr>
<td class="tg-yw4l">{{ projects.projectTitle }}</td>
<td class="tg-yw4l">{{ projects.projectContact }}</td>
<td class="tg-yw4l">{{ projects.projectValueProposed }}xx{{projects.id }}</td>
<td class="tg-yw4l">{{ project_task}}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No projects are available.</p>
{% endif %}
I am using Admin to create Project and add projectTask which works well.
I have two options:
1. To update the value of projectValueActual in the Project when i save the Admin page
2. To display dynamically the Project and sum of related ProjectTaskAmount
Need some help on how to prepare the values in the view (if that is the correct place to do it) and pass the context in some way i can display it.
The names of the model properties can be made much less verbose. Also, adding a related_name to the ForeignKey, so you can directly reference it from the Project model.
class Project(models.Model):
contact = models.ForeignKey(Contact)
title = models.CharField(max_length=100)
address1 = models.CharField(max_length=200)
valueProposed = models.IntegerField(default=0)
valueActual = models.IntegerField(default=0)
def __str__(self):
return self.title
class ProjectTask(models.Model):
project = models.ForeignKey(Project, related_name='tasks')
description = models.CharField(max_length=200)
contact = models.ForeignKey(Contact)
amount = models.IntegerField(default=0)
def __str__(self):
return self.description
That makes the view much simpler
def index(request):
ctx = {'project_list': Project.objects.all()}
return render(request, 'construction/index.html', ctx)
I am not quite sure what you want to do in your template, but lets assume you want to list all Tasks under each related Projects and then print the sum of the ProjectTask.amount values.
{% if project_list %}
<table class="tg">
<tr>
<th class="tg-le8v">Project name</th>
<th class="tg-le8v">Customer</th>
<th class="tg-le8v">Proposed Value</th>
<th class="tg-le8v">Actual Value</th>
</tr>
{% for project in project_list %}
<tr>
<td class="tg-yw4l">{{ project.title }}</td>
<td class="tg-yw4l">{{ project.contact }}</td>
<td class="tg-yw4l">{{ project.valueProposed }}xx{{projects.id }}</td>
<td class="tg-yw4l">
{% for task in project.tasks %}
<span>{{ task.amount }}</span>
{% if forloop.last %} = {% else %} + {% endif %}
{% endfor %}
{{ project.sumTaskAmounts }}
</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No projects are available.</p>
{% endif %}
And finally, to easily get the sum of all the Task.amount properties, you need to add a #property to Project that returns the sum of the amount values for each project's ProjectTask instances.
class Project(models.Model):
...
#property
def sumTaskAmounts(self):
return self.tasks.aggregate(amount=Sum('amount')).amount
Didn't test any of it, but that should more or less do it.
I've made the following changes to implement updating when i save the admin form.
models.py
def update_total_amount(self):
total=0
total=self.task.aggregate(amount=Sum('amount'))
self.valueActual=total['amount']
self.save()
and in admin.py
def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)
for instance in instances:
instance.save()
formset.save_m2m()
instance.project.update_total_amount()
works for what I wanted to do. thanks for the help.

Categories