I am currently using django 3.1.3 on Windows 10.
I was following Dennis Ivy’s tutorials for creating a django app, and I reached part 11: Inline Formsets. I am 100% sure I have everything Dennis has but for some reason I am getting the error I mentioned in the title. It was able to access the dashboard once, when I went to update orders it crashed, and after that I was never even able to access dashboard (http://127.0.0.1:8000/dashboard/) again! Here are my files:
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import inlineformset_factory
from .models import Customer, Address, Credit_Card, Order, Grocery_Store, Product, Supplier, Warehouse, Staff_Member, Price
from .forms import OrderForm
#Create your views here.
def index(request):
return render(request, 'index.html', {})
def dashboard(request):
orders = Order.objects.all()
customers = Customer.objects.all()
total_customers = customers.count()
total_orders = orders.count()
delivered = orders.filter(status='Delivered').count()
pending = orders.filter(status='Pending').count()
context = {'orders':orders, 'customers':customers,'total_orders':total_orders,'delivered':delivered,'pending':pending }
return render(request, 'dashboard.html', context)
def customer(request, pk_test):
customer = Customer.objects.get(Customer_ID=pk_test)
orders = customer.order_set.all()
order_count = orders.count()
context = {'customer':customer, 'orders':orders, 'order_count':order_count}
return render(request, 'customer.html', context)
def products(request):
products = Product.objects.all()
return render(request, 'products.html', {'products':products})
def createOrder(request, pk):
OrderFormSet = inlineformset_factory(Customer, Order, fields=('Product_Name', 'status'), extra=10 )
customer = Customer.objects.get(Customer_ID=pk)
formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
#form = OrderForm(initial={'customer':customer})
if request.method == 'POST':
#print('Printing POST:', request.POST)
#form = OrderForm(request.POST)
formset = OrderFormSet(request.POST, instance=customer)
if formset.is_valid():
formset.save()
return redirect('/dashboard')
context = {'form':formset}
return render(request, 'order_form.html', context)
def updateOrder(request, pk):
order = Order.objects.get(Order_Number = pk)
form = OrderForm(instance = order)
if request.method == 'POST':
form = OrderForm(request.POST, instance = order)
if form.is_valid():
form.save()
return redirect('/dashboard')
context = {'form':form}
return render(request, 'order_form.html', context)
def deleteOrder(request, pk):
order = Order.objects.get(Order_Number = pk)
if request.method == "POST":
order.delete()
return redirect('/dashboard')
context = {'item':order}
return render(request, 'delete.html', context)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = "index"),
path('dashboard/', views.dashboard, name = "dashboard"),
path('customer/<str:pk_test>/', views.customer, name = "customer"),
path('products/', views.products, name = "products"),
path('create_order/<str:pk>/', views.createOrder, name = "create_order"),
path('update_order/<str:pk>/', views.updateOrder, name = "update_order"),
path('delete_order/<str:pk>/', views.deleteOrder, name = "delete_order"),
]
forms.py
from django.forms import ModelForm
from .models import Order
class OrderForm(ModelForm):
class Meta:
model = Order
fields = '__all__'
dashboard.html
{% extends 'base.html' %}
{% block content %}
{% include 'status.html' %}
<br>
<div class="row">
<div class="col-md-5">
<h5>CUSTOMERS:</h5>
<hr>
<div class="card card-body">
<a class="btn btn-primary btn-sm btn-block" href="">Create Customer</a>
<table class="table table-sm">
<tr>
<th>Customer</th>
<th>Account Balance</th>
</tr>
{% for customer in customers %}
<tr>
<td><a class="btn btn-sm btn-info" href="{% url 'customer' customer.Customer_ID %}">View</a></td>
<td>{{customer.First_name}} {{customer.Last_name}}</td>
<td>{{customer.Account_Balance}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
<div class="col-md-7">
<h5>LAST 5 ORDERS</h5>
<hr>
<div class="card card-body">
<table class="table table-sm">
<tr>
<th>Product</th>
<th>Date Orderd</th>
<th>Status</th>
<th>Update</th>
<th>Remove</th>
</tr>
{% for order in orders %}
<tr>
<td>{{order.Product_Name}}</td>
<td>{{order.date_created}}</td>
<td>{{order.status}}</td>
<td><a class="btn btn-sm btn-info" href="{% url 'update_order' order.Order_Number %}">Update</a></td>
<td><a class="btn btn-sm btn-danger" href="{% url 'delete_order' order.Order_Number %}">Delete</a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
order_forms.html
{% extends 'base.html' %}
{% load static %}
{% block content %}
<div class="row">
<div class="col-md-30">
<div class="card card-body">
<form action="" method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for field in form %}
{{field}}
<hr>
{% endfor %}
<input type="submit" name="Submit">
</form>
</div>
</div>
</div>
{% endblock %}
customer.html
{% extends 'base.html' %}
{% block content %}
<br>
<div class="row">
<div class="col-md">
<div class="card card-body">
<h5>Customer:</h5>
<hr>
<a class="btn btn-outline-info btn-sm btn-block" href="">Update Customer</a>
<a class="btn btn-outline-info btn-sm btn-block" href="{% url 'create_order' customer.Customer_ID %}">Place Order</a>
</div>
</div>
<div class="col-md">
<div class="card card-body">
<h5>Total Orders</h5>
<hr>
<h1 style="text-align: center;padding: 10px"></h1>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col">
<div class="card card-body">
<form method="get">
<button class="btn btn-primary" type="submit">Search</button>
</form>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md">
<div class="card card-body">
<table class="table table-sm">
<tr>
<th>Product</th>
<th>Category</th>
<th>Date Orderd</th>
<th>Status</th>
<th>Update</th>
<th>Remove</th>
</tr>
{% for order in orders %}
<tr>
<td>{{order.Product_Name}}</td>
<td>{{order.Product_Name.category}}</td>
<td>{{order.date_created}}</td>
<td>{{order.status}}</td>
<td><a class="btn btn-sm btn-info" href="{% url 'update_order' order.Order_Number %}">Update</a></td>
<td><a class="btn btn-sm btn-danger" href="{% url 'delete_order' order.Order_Number %}">Delete</a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}
Is it that I am probably running a newer Django version, or I am blindly missing something. Need help fast!
The following is the full Traceback error
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/
Django Version: 3.1.3
Python Version: 3.8.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ogs_features']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\CS425-Databases\Project\ogs\online-grocery-store\ogs_features\templates\base.html, error at line 41
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order/(?P<pk>[^/]+)/
```]
31 : background-color: #7CD1C0;
32 : }
33 : </style>
34 : <link rel = "stylesheet" type="text/css" href = "{% static '/css/base.css' %}">
35 :
36 : </head>
37 : <body>
38 : <nav class="navbar navbar-expand-lg navbar-light bg-light">
39 : <img src = "{% static 'images/logo.png' %}" alt = "Our logo">
40 : <a class="navbar-brand" href="{% url 'index' %}">Grocery Store</a>
41 : <button c lass="navbar-toggler" type="button" data-to ggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
42 : <span class="navbar-toggler-icon"></span>
43 : </button>
44 :
45 : <div class="collapse navbar-collapse" id="navbarSupportedContent">
46 : <ul class="navbar-nav mr-auto">
47 :
48 : <li class="nav-item">
49 : <a class="nav-link" href="{% url 'dashboard' %}">Dashboard</a>
50 : </li>
51 :
Traceback (most recent call last):
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\CS425-Databases\Project\ogs\online-grocery-store\ogs_features\views.py", line 20, in dashboard
return render(request, 'dashboard.html', context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\defaulttags.py", line 211, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\template\defaulttags.py", line 446, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\urls\base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\zainf\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\django\urls\resolvers.py", line 685, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /dashboard/
Exception Value: Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) t