Hello Django Forum! I was creating forms using a common template and a part of the form creation is creating views that I than link to my urls. Createview views appear easy to create. However, UpdateView subclassed Views have proven to be a different challenge as they are asking for the field names. Codecademy tells me to use the fields names used in createview but the thing is, I never had a fieldname attribute in my CreateViews subclassed views that utlized a fieldname attribute. I have nothing to go on. I am wondering what I am supposed to put for the fieldname.
I am also having an issue with my finance function view, which says it needs a request as a parameter. I am having that issue in urls.py as when I run my code it gives the views.finance() error type error finance() missing one required argument.
urls.py
"""
URL configuration for djangodelights project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
# from inventory.views import finance, home, IngredientsView, MenuView, PurchaseView
from inventory import views # with every view imported you need to specify views.viewname as seen in this file in the code below
from django.views.generic.base import TemplateView
from django.http import HttpResponse
urlpatterns = [
path('', views.home, name='default'), # users don't need to see the rocket page anyway. they need to see the home page.
path('admin/', admin.site.urls),
path('finance/', views.finance, name='finance'),
path('home/', views.home, name='home'), #I am attempting to connect the home_view function with the views function.
path('ingredients/', views.IngredientsView.as_view(), name='ingredients'),
path('menu/', views.MenuView.as_view(), name='menu'),
path('purchases/', views.PurchaseView.as_view(), name='purchases'),
path('menu/add',views.MenuAdditionView.as_view(), name="menuadd"),
path('ingredient/add', views.IngredientAdditionView.as_view(), name='ingredientadd'), # if class based view it requires an as_view
# path('ingredient/update', views.UpdateIngredientView.as_view(), name='ingredientupdate'),
path('recipe/add', views.RecipeAdditionView.as_view(), name='recipeadd'),
# path('finance', views.finance(), name='finance'), # this line of code holds the error.
# finance is not a classed based view therefore i do not need an as_view
]
forms.py
from django import forms
from .models import MenuItem, Ingredient, Purchases, RecipeRequirement
class MenuAdditionForm(forms.ModelForm):
class Meta:
model = MenuItem
fields = ('__all__')
class IngredientAdditionForm(forms.ModelForm):
class Meta:
model = Ingredient
fields = ('__all__')
class UpdateIngredientForm(forms.ModelForm):
class Meta:
model = Ingredient
fields = ('__all__')
class RecipeAdditionForm(forms.ModelForm):
class Meta:
model = RecipeRequirement
fields = ('__all__')
class Purchases(forms.ModelForm):
class Meta:
model = Purchases
fields = ('__all__')
views.py
from django.shortcuts import render
from django.urls import path
from .models import Ingredient, MenuItem, Purchases, RecipeRequirement
from django.views.generic import ListView
# import one at the time write the view for it hook it up in urls.py check if it works than move on to the next one.
from .forms import MenuAdditionForm, IngredientAdditionForm, UpdateIngredientForm, RecipeAdditionForm # link the views and forms togather on this line
# errors can come from importing on the wrong django.views
from django.views.generic.edit import CreateView, UpdateView
from django.http import HttpResponse
# Create your views here.
def finance(request):
purchased_items = Purchases.objects.all() # this grabs every row in the purchases table
total_revenue = 0
for item in purchased_items:
total_revenue = total_revenue + item.menu_order.price #this uses the price of each item to calculate the revenue
print(str(total_revenue))
total_cost= ingredient_cost_calculate()
print(str(total_cost) + " this is the total cost calculation")
total_profit= total_revenue - total_cost
print(str(total_profit) + "total profit calculation amount")
return render(request,'inventory/finance.html', {"total_revenue": total_revenue, "total_cost":total_cost, "total_profit": total_profit})
# i want the price of the item and the cost of each item.
# what holds this information? my ingredient model.
# it holds the price per unit and the unit.
def ingredient_cost_calculate():
# classes do not store information objects do.
# i need the cost per ingredient multiplied by quantity used.
# I need to than for loop over all of the ingredients in a recipe and than apply the cost per ingredient function or
# the same mathematical operation.
# classes do not have the data, the objects do.
# when the customer purchases something an entry is made in the purchases table.
# Purchases.objects.all() grabs all of the Purchases data and puts it in a list.
purchases_objects = Purchases.objects.all()
# I need to find all of the ingredients per order. How do I get one order to find the ingredients on.
# I need to use a for loop to access each purchase.
menu_order_cost = 0
for purchase in purchases_objects:
menu_item_object = purchase.menu_order
# recipe requirements is a list of data
recipe_requirements = RecipeRequirement.objects.filter(recipe=menu_item_object)
# we iterate over that list with a for loop and access the iteration variable quantity fields.
for requirement in recipe_requirements:
cost_per_ingredient = requirement.quantity * requirement.ingredient.price_per_unit
menu_order_cost = menu_order_cost + cost_per_ingredient
return menu_order_cost
# the purchases are listed by names they do hold the ingredient data for each indvidual order.
def home(request):
return render(request,'inventory/home.html')
class MenuView(ListView):
# when we specify the model being used for the template its almost as if we import or give access
# to the html template the class data for us to use for loops and django code on.
model = MenuItem
template_name = 'inventory/menu.html'
class PurchaseView(ListView):
model = Purchases
template_name = 'inventory/purchases.html'
class IngredientsView(ListView):
model = Ingredient
template_name = "inventory/ingredients.html"
class MenuAdditionView(CreateView):
model = MenuItem
template_name = "inventory/form_template.html"
form_class = MenuAdditionForm
class IngredientAdditionView(CreateView):
model = Ingredient
template_name = 'inventory/form_template.html'
form_class = IngredientAdditionForm
class UpdateIngredientView(UpdateView): # I am thinking this is an UpdateView
model = Ingredient
template_name = 'inventory/form_template.html'
# fields = [] I don't know what to put for fields. Codecademy says put in the same field names as create view but create view does not hold any field names.
class RecipeAdditionView(CreateView):
model = RecipeAdditionForm
template_name = 'inventory/form_template.html'
# update view will require a <pk> or slug where I specify what I need to update.
# check out the codecademy section.
# https://www.codecademy.com/paths/build-python-web-apps-with-django/tracks/views-in-django/modules/django-writing-more-views/lessons/django-views/exercises/using-primary-keys-in-urls
# create a view based on the forms made.
# create a path to urls.py