Reverse for 'update_order' with arguments '('',)' not found. 1 pattern(s) tried: ['update_order/(?P<pk>[^/]+)/$']

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

Check to see if you have an order that has a blank or null Order_Number.

I realized this might be the issue, take a look at mu admin page, I have two orders I am able to edit and one that is non editable and I can’t even delete it.

How do I take care of this if I can’t even delete it in the admin page?

I need to resolve this as soon as possible.
Thank you

Go into the database directly and delete that row.
You don’t mention what database you’re using, so the precise method for that may vary.
If you’re not comfortable with running SQL from your command line tools, look for a GUI / browser-based UI such as pg-admin for PostgreSQL.
Did you do some type of modification to your database (Model) after adding that row? (I’m not familiar with those tutorials so I don’t know what things should be, compared to how they are.)

1 Like

Thank you, I knew this had something to do with the error, I accessed my postgresql database and deleted the order from all table sit existed in and that fixed it!

I have some trouble with one of my functions in my projects view.
I have tested with removing this function and then things work.
Here is the view,

def updatePurchaseOrder(request, pk):

update_order = Purchase_Order.objects.get(id=pk)

form = PurchaseOrderForm(instance=update_order)

if request.method == 'POST':

    form = PurchaseOrderForm(request.POST, instance=update_order)

    if form.is_valid():

        form.save()

        return redirect('/')

       

context = {'form':form}

return render(request, 'order_form.html', context)

Here is the link which tries to access the view

{% for order in orders %}

                <tr>

                    <td>{{order.po_number}}</td>

                    <td>{{order.product}}</td>

                    <td>{{order.status}}</td>

                    <td><a href="{% url 'update_order' update_order.id %}">Update</a></td>

                    <td><a href="">Delete</a></td>

                </tr>

            {% endfor %}

I also have attached the urls.py

urlpatterns = [

path('admin/', admin.site.urls),

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

path('supplier/<str:pk>/', views.supplier, name="supplier"),

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

path('purchase_order/', views.purchase_order, name="purchase_order"),

path('order_form/', views.createPurchaseOrder, name="create_purchase_order"),

path('update_order/<str:pk>/', views.updatePurchaseOrder, name="update_order"),

]

I expect there to be a simple mistake, but can’t seem to find it.

Since this is a different issue with different code, I would suggest you open up a new topic to discuss this.

When posting code, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.
This forces the forum software to keep your code properly formatted, which is critical with Python. (Also, please try to avoid having the code double-spaced - it makes it really tough to read it here.)

Just change every “order.Order_Number” to this “order.id”

1 Like