hello my best Django friend here, would you please help to fix this issue.
my goal is hit Update => url ‘update_order’ order.id
the button update located in template dashboard.html see section4
the urls.py see section3
the views.py see section2
the error info showed section 1
many thanks, I am from China Shanghai.
section1
NoReverseMatch at /
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order/(?P<pk>[^/]+)/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.0.8
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order/(?P<pk>[^/]+)/$']
Exception Location: /Users/charles/Desktop/Mac_2020_log/2020cw28/0711/venv/lib/python3.8/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 677
Python Executable: /Users/charles/Desktop/Mac_2020_log/2020cw28/0711/venv/bin/python
Python Version: 3.8.3
Python Path:
['/Users/charles/Desktop/Mac_2020_log/2020cw28/0711/app/crm1',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Users/charles/Desktop/Mac_2020_log/2020cw28/0711/venv/lib/python3.8/site-packages']
Server time: Thu, 23 Jul 2020 14:42:43 +0000
Error during template rendering
In template /Users/charles/Desktop/Mac_2020_log/2020cw28/0711/app/crm1/accounts/templates/accounts/main.html, error at line 30
Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order/(?P<pk>[^/]+)/$']
section 2
from django.shortcuts import render, redirect
from .models import *
from .forms import OrderForm
def home(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, 'accounts/dashboard.html', context)
def products(request):
products = Product.objects.all()
return render(request, 'accounts/products.html', {'products': products})
def customer(request, pk_test):
customer = Customer.objects.get(id=pk_test)
orders = customer.order_set.all()
order_count = orders.count()
context = {'customer':customer, 'orders':orders, 'order_count':order_count}
return render(request, 'accounts/customer.html', context)
def createOrder(request):
form = OrderForm()
if request.method == 'POST':
#print('Printing POST:' , request.POST)
form = OrderForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
context = {'form':form}
return render(request, 'accounts/order_form.html', context)
def updateOrder(request, pk):
order = Order.objects.get(id=pk)
form = OrderForm(instance=order)
context = {'form':form}
return render(request, 'accounts/order_form.html', context)
section 3
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('products/', views.products, name="products"),
path('customer/<str:pk_test>/', views.customer, name="customer"),
path('create_order/', views.createOrder, name="create_order"),
path('update_order/<str:pk>/', views.updateOrder, name="update_order"),
]
section 4
{% extends 'accounts/main.html' %}
{% block content %}
{% include 'accounts/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>Phone</th>
</tr>
{% for i in customers %}
<tr>
<td>{{ i.name }}</td>
<td>{{ i.phone }}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
<div class="col-md-7">
<h5>LAST 5 ORDERS</h5>
<hr>
<div class="card card-body">
<a class="btn btn-primary btn-sm btn-block" href="{% url 'create_order' %}">Create Order</a>
<table class="table table-sm">
<tr>
<th>Product</th>
<th>Date Orderd</th>
<th>Status</th>
<th>Update</th>
<th>Remove</th>
</tr>
{% for i in orders %}
<tr>
<td>{{ i.product }}</td>
<td>{{ i.date_created }}</td>
<td>{{ i.status }}</td>
<td><a class="btn btn-sm btn-info"href="{% url 'update_order' order.id %}">Update</a></td>
<td><a class="btn btn-sm btn-danger"href="">Delete</a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
{% endblock %}