the current path didn't match any of these

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 %}

Please don’t post screenshots for code fragments. They’re not easy to read on all devices and the key text can’t be recopied in responses to discuss specific lines of code.
When copying code, please enclose it between two lines consisting only of three backtick - ` characters. (This means you’ll have one line that is just ```, then your code, then another line of ```. Be sure you use the backtick (`) character and not the apostrophe (’).

You have a space in { %. It’s supposed to be {% Spaces aren’t allowed between the brace character { and the percent character %.

3 Likes

many thanks.

I delete the space, there is a new error info.

This means you still have an error in your {% url 'update_order' ... directive.

1 Like

This is your chunk for looping over the orders:

				{% 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 %}

Note that you start the loop by using {% for i in orders %}. So in each iteration, the current element in the ‘orders’ list/QuerySet is referred to as i. And you do use this for the most part, e. g. {{ i.product }}. But then for the url part you write "{% url 'update_order' order.id %}". Django doesn’t “know” that you here are referring to the iteration’s ‘orders’ element. You still have to refer to it as i, because that is what you defined it as when starting the for loop. So, you need to replace order here with i.

Edit: Alternatively, you could replace all the i's with order ({% for order in orders %} et c.), which I think is clearer and makes debugging or adding things later on simpler. If I were you, I’d do that and also use customer instead of i in the {% for i in customers %}... loop.

1 Like

thanks so much.
your comment ‘order’ and ‘customer’ instead of ‘i’ was done.
it works.
you guys are great.

Hello @chunboxing I’ve been following the same tutorial as you did. and getting error in naming the urls. would you please help to fix this issue.
I can’t get the static urls paths for dashboard and product page
The navbar.html page is where i write them with naming of urls.

Blockquote from django.urls import path

from . import views

urlpatterns = [

path('products/',views.Product, name='products'),

path('customers/<int:pk>/',views.Customers,name="customerpages"),

path('',views.Home, name="home"),

]

Please Help!! @KenWhitesell

Getting this error.

Please don’t post screenshots for code fragments. They’re not easy to read on all devices and the key text can’t be recopied in responses to discuss specific lines of code.
When copying code, please enclose it between two lines consisting only of three backtick - ` characters. (This means you’ll have one line that is just ```, then your code, then another line of ```. Be sure you use the backtick (`) character and not the apostrophe (’).

The quick answer is that it doesn’t look like Django is rendering your template. We’ll need to see the view that is generating the page that you’re clicking on that is causing the error, along with the template being rendered to try and figure out why.

Thank you @KenWhitesell I figured out the error.

can you tell me how did you fixed it.
my email:asfaqahmed356@gmail.com