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

Django编辑模型表单问题:Reverse for '变量名'未找到报错排查

问题解决

错误原因

Django 模板的 url 标签通过 urls.py 中路由配置的 name 参数反向匹配地址,你给编辑线索路由配置的名称是 edit_lead,但模板中调用时填写的名称是 project_id,二者不匹配就会触发该报错。

修复步骤

  1. 修改dashboard.html中的链接代码:
    将原代码
<td><a href="{% url 'project_id' lead.project_id %}">{{lead.project_id }}</a></td>

替换为:

<td><a href="{% url 'edit_lead' lead.project_id %}">{{lead.project_id }}</a></td>
  1. 适配UpdateView的参数规则:
    Django的UpdateView默认从路由参数中读取pk作为主键查询数据,如果你的project_id是模型Leads的主键,需要在视图中指定参数名,修改views.py中的LeadUpdate类:
class LeadUpdate(UpdateView):
    model = Leads
    fields='__all__'
    template_name_suffix = '_update_form'
    pk_url_kwarg = 'project_id'

如果project_id不是主键,是普通唯一字段,则改用如下配置:

class LeadUpdate(UpdateView):
    model = Leads
    fields='__all__'
    template_name_suffix = '_update_form'
    slug_url_kwarg = 'project_id'
    slug_field = 'project_id'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 14:15:01