You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django报错:Reverse for 'people' not found 求助(附代码)

Django URL反向解析错误:无法匹配目标路由

我在尝试添加URL链接跳转到个人详情页时出错了,我是Django新手,可能有低级错误,麻烦帮忙看看哪里出问题。

views.py

from django.shortcuts import  render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import People

# Create your views here.
class pplList(ListView):
    model = People
    context_object_name = 'people'

class pplDetail(DetailView):
    model = People
    context_object_name ='cnd'
    template_name = 'base/people.html'

people_list.html

<h1>Interviewee Dashboard</h1>

<table>
    <tr>
        <th> Item</th>
        <th> </th>

    </tr>
    {% for cnd in people %}
    <tr>
        <td>{{cnd.name}}</td>
        <td><a href="{% url 'people' cnd.id %}">View</a></td>
    </tr>
    {% empty %}
    <h3>No items in list</h3>
    {% endfor %}
</table>

models.py

from unittest.util import _MAX_LENGTH
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
status_choices = (
    ("Choose", "Choose"),
    ("Resume Submitted","Resume Submitted"),
    ("Resume Reviewed", "Resume Reviewed"),
    ("Interview Scheduled","Interview Scheduled" ),
    ("Interview Completed","Interview Completed" ),
   
    
)
wl = (
    ("Choose", "Choose"),
    ("Accepted", "Accepted"),
    ("Rejected", "Rejected"),
)

class People(models.Model):
   # user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200)
    age = models.IntegerField(null=True, blank=True)
    address = models.TextField(null=True, blank=True)
    position = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    status = models.CharField(
        max_length = 20,
        choices = status_choices,
        default = '1'
        )
    hired = models.CharField(
        max_length = 20,
        choices = wl,
        default = '1'
        )

    def __str__(self):
        return self.name
    class Meta: 
        ordering = ['hired']

base应用下的urls.py(非根路由)

from django.urls import path
from .views import pplList, pplDetail

urlpatterns = [
    path('', pplList.as_view(), name='ppl'),
    path('people/<int:pk>', pplDetail.as_view(), name='ppl'),
]

问题原因及修复方案

1. 核心问题:路由名称不匹配+重复命名

  • 模板里用了{% url 'people' cnd.id %},但urls.py里根本没有名为people的路由,反而给列表页和详情页都起了相同的名字ppl,导致Django无法正确解析反向路由。
  • 详情页路由未遵循Django规范添加结尾斜杠,可能引发路由匹配异常。

2. 修复步骤

修改urls.py

给两个路由设置唯一名称,并修正详情页路由格式:

from django.urls import path
from .views import pplList, pplDetail

urlpatterns = [
    path('', pplList.as_view(), name='ppl_list'),
    path('people/<int:pk>/', pplDetail.as_view(), name='ppl_detail'),
]

修改模板people_list.html

使用正确的路由名称进行反向解析:

<td><a href="{% url 'ppl_detail' cnd.id %}">View</a></td>

3. 额外优化点

模型里status和hired字段的default设置为'1',但选项中没有对应值,会导致数据库存储异常,建议改成选项内存在的默认值:

status = models.CharField(
    max_length = 20,
    choices = status_choices,
    default = "Choose"
    )
hired = models.CharField(
    max_length = 20,
    choices = wl,
    default = "Choose"
    )

内容的提问来源于stack exchange,提问作者Vignesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 02:10:51