【问题标题】:Python Django NoReverseMatch Error When I {% url 'client_detail' client.id %}当我 {% url 'client_detail' client.id %} 时出现 Python Django NoReverseMatch 错误
【发布时间】:2021-03-07 15:21:12
【问题描述】:

我知道我的问题对其他人来说似乎很熟悉,但事实并非如此。当我访问我的主页时,我被困在下面的错误中。问题是 {% url 'client_detail' client.id %}。我很确定我做得对,但它没有发生。

NoReverseMatch 在 / 未找到带有参数 '('',)' 的 'client_detail' 的反向操作。尝试了 1 种模式:['client/(?P[0-9]+)/$']

非常感谢任何帮助

models.py

from django.db import models
from django.utils import timezone  
from django.contrib.auth.models import User  
from django.urls import reverse

class Client(models.Model):
    client_name= models.CharField(max_length=100) 
    client_sector= models.CharField(max_length=100) 
    client_phone= models.CharField(max_length=100) 
    client_city= models.CharField(max_length=100) 
    client_district= models.CharField(max_length=100) 
    client_adress= models.TextField() 
    client_type= models.CharField(max_length=100) 
    client_priority= models.CharField(max_length=100)
    client_first_contacted_date=models.DateTimeField(default=timezone.now)  

views.py

from django.shortcuts import render
from .models import Client 
from django.views.generic import ListView, DetailView

def ClientView(request):
    context = {'client_aranacak': Client.objects.all()}
    return render(request, 'crm/crm_home.html', context)

class ClientListView(ListView):  
    model = Client  
    template_name = 'crm/crm_home.html'  
    context_object_name = 'client_aranacak'  
    ordering = ['-client_first_contacted_date']

class ClientDetailView(DetailView):
    model = Client

urls.py

from django.urls import path
from . import views
from .views import ClientListView, ClientDetailView

urlpatterns = [
    path('', ClientListView.as_view(), name='crm_client'),
    path('client/<int:pk>/', ClientDetailView.as_view(), name='client_detail'),
]

# {% url 'crm_client_detail' client.id %}

crm_home.html

{%extends "base.html"%}
{%block content%}
<div class="card mb-4">
    <div class="card-header">
        <i class="fas fa-table mr-1"></i>
        Customers
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <td><strong>Client Name</strong></td>
                        <td><strong>Client Sector</strong></td>
                        <td><strong>Client Phone</strong></td>
                        <td><strong>Client City</strong></td>
                        <td><strong>Client District</strong></td>
                        <td><strong>Client Adress</strong></td>
                        <td><strong>Client Type</strong></td>
                        <td><strong>Client Priority</strong></td>
                        <td><strong>Client First Contacted Date</strong></td>
                        <td><strong>Client ID</strong></td>
                    </tr>
                </thead>
                <tbody>
                    {%for i in client_aranacak%}
                        <tr>
                            <td><a href="{% url 'client_detail' client.id %}">{{i.client_name}}</a></td>
                            <td>{{i.client_sector}}</td>
                            <td>{{i.client_phone}}</td>
                            <td>{{i.client_city}}</td>
                            <td>{{i.client_district}}</td>
                            <td>{{i.client_adress}}</td>
                            <td>{{i.client_type}}</td>
                            <td>{{i.client_priority}}</td>
                            <td>{{i.client_first_contacted_date}}</td>
                            <td>{{i.client.id}}</td>
                        </tr>
                    {%endfor%}
                </tbody>
            </table>
        </div>
    </div>
</div>
{%endblock content%}

【问题讨论】:

    标签: python html django hyperlink


    【解决方案1】:

    查看您的 crm/crm_home.html 文件并修复如下:&lt;td&gt;&lt;a href="{% url 'client_detail' i.id %}"&gt;{{i.client_name}}&lt;/a&gt;&lt;/td&gt;

    【讨论】:

    • 非常感谢您。我真的很感谢你的帮助。这解决了我的 for 循环。我真是个白痴错过它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多