首页 > 解决方案 > NoReverseMatch 在 / | 模板渲染期间出错

问题描述

未找到带有参数“(”,)“的“客户”的反向操作。尝试了 1 种模式:['customer/(?P[^/]+)/$']


这是错误
请求方法:GET 请求 URL: http: //127.0.0.1 :8000/ Django 版本:3.2.4 异常类型:NoReverseMatch 异常值:
“客户”的反向参数“(”,)“不是成立。尝试了 1 种模式:['customer/(?P[^/]+)/$'] 异常位置:C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\ django\urls\resolvers.py,第 694 行,在 _reverse_with_prefix Python 可执行文件:C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe Python 版本:3.9.5 Python 路径:
['C:\Users\user\Downloads\Git Project\firstwebsite', 'C:\Users\user\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\user\AppData\ Local\Programs\Python\Python39\DLLs','C:\Users\user\AppData\Local\Programs\Python\Python39\lib','C:\Users\user\AppData\Local\Programs\Python\Python39' , 'C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages'] 服务器时间:2021 年 6 月 9 日星期三 06:20:15 +0000

这是代码
urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.dashboard, name="dashboard"),
    path('customer/<str:pk>/', views.customer, name="customer"),
    path('product/', views.product, name="product"),
]


这是models.py

from django.db import models

# Create your models here.

class Customer(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.FloatField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Tag(models.Model):
    name = models.CharField(max_length=200, null=True)
    
    def __str__(self):
        return self.name

class Product(models.Model):
    CATEGORY = (
        ('Indoor', 'Indoor'),
        ('Outdoor', 'Outdoor')
        )
    name = models.CharField(max_length=200, null=True)
    price = models.FloatField(null=True)
    tag = models.ManyToManyField(Tag)
    category = models.CharField(max_length=200, null=True, choices=CATEGORY)
    description = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    
    def __str__(self):
        return self.name

class Order(models.Model):

    #we create a dropbox like this
    STATUS = (
        ('Pending', 'Pending'),
        ('Out Of Deliery', 'Out Of Delivery'),
        ('Deliverder', 'Delivered'),
        )
    customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)

    date_crated = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=200,null=True, choices=STATUS)

    def __str__(self):
        return self.status


这是views.py

from django.shortcuts import render
from django.http import HttpResponse

from .models import Customer, Order, Product


# Create your views here.
def dashboard(request):
    customer = Customer.objects.all()
    orders = Order.objects.all()
    total_customer = customer.count()
    total_orders = orders.count()
    delivered = orders.filter(status='delivered').count()
    pending = orders.filter(status='pending').count()
    

    context = {'orders':orders, 'total_orders':total_orders, 'total_customer':total_customer, 'pending':pending,'delivered':delivered, 'customer':customer}
    return render(request, "accounts/dashboard.html", context)

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

def customer(request, pk):
    customer = Customer.objects.get(id=pk)
    orders = customer.order_set.all()
    order_count = orders.count()
    context = {'customer':customer, 'orders':orders, 'order_count':order_count}
    return render(request, 'accounte/contact.html', context)


模板 Dashboard.html(html 文件)

{% extends 'accounts/main.html' %}

{% block content %}

{% load static %}


    <div class="center" style="display: block; margin-left: auto; margin-right: auto; width: 50%; ">
        <img src="{% static 'images/welcome.png' %}" class="img-fluid" alt="">
    </div> 
    <br>
    <br>
    <br>
    
    <br>  
    <div class=" justify-content-center" style="padding: 20; width: 60%; margin-right: auto; margin-left: auto;">
        <h1 class="display-1 " style="text-align: center; font-style: italic;">This is my first website</h1>
        <div style="padding-top: 50;">
            <img src="{% static 'images/186.png' %}" alt="" style="display: block; margin-left: auto; margin-right: auto; width: 50%; ">
        </div>
    </div> 
    
    <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 Customers</a>
            <table class="table table-sm">
                <tr>
                    
                    <th>Customers: </th>
                    <th>Phone no: </th>
                    
                </tr>
                {% for customers in customer %}
                    <tr>
                        <th><a class="btn btn-sm btn-info" href="{% url 'customer' customer.id %}">view</a></th>
                        <td>{{customers.name}}</td>
                        <td>{{customers.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="">Create Orders</a>
            <table class="table table-sm">
                <tr>
                    
                    <th>Product</th>
                    <th>category</th>
                    <th>Date Ordered</th>
                    <th>Status</th>
                    <th>Update</th>
                    <th>Remove</th>
                </tr>

                {% for i in orders %}
                    <tr>
                        <td>{{i.product}} </td>
                        <td>{{i.product.category}} </td>
                        <td>{{i.date_crated}} </td>
                        <td>{{ i.status }} </td>
                        
                        <td><a href="#">Update</a></td>
                        <td><a href="#">Delete</a></td>
                      
                    </tr>
                    
                {% endfor %}
            
              
            </table>
        </div>
    </div>
    {% include 'accounts/status.html' %}
    


</div>


{% endblock %}**

idk为什么我会出错。帮帮我!

标签: pythondjango

解决方案


推荐阅读