Home > Net >  NoReverseMatch Django Project
NoReverseMatch Django Project

Time:10-01

I want to create the products through the customer's profile, so that the customer's name is attached to the form when creating the product that is related to it. But when I try to enter the user profiles from the dashboard it throws this error

urls.py

    urlpatterns = [
    path('', views.home, name="home"),
    path('products/', views.products, name="products"),
    path('customer/<str:pk_test>/', views.customer, name="customer"),

    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'),
    path('create_customer/', views.createCustomer, name='create_customer'),
]

views.py

Here I focus on the "create Order" function passing the customer's primary key and using "initial" to append the customer's name to the form

  def home(request):
    orders_value = Order.objects.all()
    customer_value = Customer.objects.all()
    
    total_orders_value = orders_value.count()
    total_customers_value = customer_value.count()

    pending_value = orders_value.filter(status='Pending').count()
    delivered_value = orders_value.filter(status='Delivered').count()

    context = {'orders_key': orders_value, 'customer_key': customer_value,
    'total_orders_key':total_orders_value, 'pending_key': pending_value, 
    'delivered_key': delivered_value}

    return render (request, 'accounts/dashboard.html', context)

def products(request):
    products_value = Product.objects.all()
    return render (request, 'accounts/products.html', {'products_key': products_value})

def customer(request, pk_test):
    customer_value = Customer.objects.get(id=pk_test)
    orders_value = customer_value.order_set.all()
    orders_value_count = orders_value.count()
    context = {'customer_key':customer_value, 'orders_key': orders_value, 'orders_key_count': orders_value_count}
    return render (request, 'accounts/customer.html', context)

def createOrder(request, pk):
    customer = Customer.objects.get(id=pk)
    form_value = OrderForm(initial={'customer':customer})
    if request.method == 'POST':
        form_value = OrderForm(request.POST)
        if form_value.is_valid:
            form_value.save()
            return redirect('/')

    context = {'form_key':form_value}
    return render(request, 'accounts/order_form.html', context)

def updateOrder(request, pk):
    order = Order.objects.get(id=pk)
    form_value = OrderForm(instance=order)
    if request.method == 'POST':
        form_value = OrderForm(request.POST, instance=order)
        if form_value.is_valid:
            form_value.save()
            return redirect('/')

    context = {'form_key':form_value}
    return render(request, 'accounts/order_form.html', context)

def deleteOrder(request, pk):
    order = Order.objects.get(id=pk)
    if request.method == 'POST':
        order.delete()
        return redirect('/')
    context={'item':order}
    return render (request, 'accounts/delete.html', context)

customer.html

{%  extends 'accounts/main.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.id %}">Place Order</a>

        </div>
    </div>

    <div class="col-md">
        <div class="card card-body">
            <h5>Contact Information</h5>
            <hr>
            <p>Email: {{customer_key.email}}</p>
            <p>Phone: {{customer_key.phone}}</p>
        </div>
    </div>

    <div class="col-md">
        <div class="card card-body">
            <h5>Total Orders</h5>
            <hr>
            <h1 style="text-align: center;padding: 10px">{{orders_key_count}}</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_key %}
                <tr>
                    <td>{{order.product}}</td>
                    <td>{{order.product.category}}</td>
                    <td>{{order.data_created}}</td>
                    <td>{{order.status}}</td>
                    <td>{{order.product}}</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="{% url 'delete_order' order.id %}">Delete</a></td>
                </tr>
                {% endfor %}

            </table>
        </div>
    </div>
</div>

{% endblock %}

dashboard.html

{% 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="{% url 'create_customer' %}">Create Customer</a>
            <table class="table table-sm">
                <tr>
                    <th></th>
                    <th>Customer</th>
                    <th>Phone</th>
                </tr>

                {% for customer in customer_key  %}
                    <tr>
                        <th><a class="btn btn-sm btn-info" href="{% url 'customer' customer.id %}">View</a></th>
                        <td>{{customer.name}}</td>
                        <td>{{customer.phone}}</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 orders in orders_key  %}
                    <tr>
                        <td>{{orders.product}}</td>
                        <td>{{orders.data_created}}</td>
                        <td>{{orders.status}}</td>
                        <td><a class="btn btn-sm btn-info" href="{% url 'update_order' orders.id %}">Update</a></td>
                        <td><a class="btn btn-sm btn-danger" href="{% url 'delete_order' orders.id %}">Delete</a></td>
                    </tr>
                {% endfor %}
        
            </table>
        </div>
    </div>

</div>

{% endblock %}

This is the error that throws me

NoReverseMatch at /customer/1/
Error during template rendering
Reverse for 'create_order' with arguments '('',)' not found. 1 pattern(s) tried: ['create_order/(?P<pk>[^/] )$']

Maybe i wrote some wrong in "customer.html" but i dont know where and i cant find the error... I need help, please

CodePudding user response:

inside your template customer.html
change this(why ? because inside your context = {'customer_key':customer_value, 'orders_key': orders_value, 'orders_key_count': orders_value_count} that refers to this template there is no key called 'customer' and you tried to access again his id that is not correct)

<a class="btn btn-outline-info  btn-sm btn-block" href="{% url 'create_order' customer.id %}">Place Order</a>

to (if you look your context you have a customer_key that is from your Customer model to get his id you can just use customer_key.pk)

<a class="btn btn-outline-info  btn-sm btn-block" href="{% url 'create_order' customer_key.pk %}">Place Order</a>

there is some mistake inside your urls.py(pk is an integer so instead of str(string) you should use int(integer))

    urlpatterns = [
    path('', views.home, name="home"),
    path('products/', views.products, name="products"),
    path('customer/<int:pk_test>/', views.customer, name="customer"),

    path('create_order/<int:pk>', views.createOrder, name='create_order'),
    path('update_order/<int:pk>', views.updateOrder, name='update_order'),
    path('delete_order/<int:pk>', views.deleteOrder, name='delete_order'),
    path('create_customer/', views.createCustomer, name='create_customer'),
]
  • Related