【问题标题】:Django NoReverseMatch with keyword argument not found没有找到带有关键字参数的 Django NoReverseMatch
【发布时间】:2023-03-15 13:10:01
【问题描述】:

感谢您的帮助。长存储短,这是我的 git repo。 https://github.com/nelsonyan/track 单击employee_list.html 第22 行中的href 时出现以下错误消息。它应该带我到employee_detail.html 进行更新,但给了我关于track_delete URL 标签的以下错误...这两个应用程序(报告,员工)有外键关联,但我不明白为什么要更新员工APP的功能与报表APP的删除功能有关。任何 Django 专家可以帮助我吗?

GET 请求 URL:http://127.0.0.1:8000/employee/update/7/Django 版本:2.0.5 异常类型:NoReverseMatch 异常值:

使用关键字参数 '{'pk': ''}' 的 'track_delete' 不反向 成立。尝试了 1 种模式:['report\/delete\/(?P[0-9]+)\/$']

异常位置: C:\Users\Nelson\Anaconda3\envs\mydjangoenv\lib\site-packages\django\urls\resolvers.py 在 _reverse_with_prefix 中,第 632 行 Python 可执行文件: C:\Users\Nelson\Anaconda3\envs\mydjangoenv\python.exe Python 版本: 3.6.6

详细来说,我正在尝试创建一个网站来跟踪艺术项目并显示公司员工信息。报告应用程序运行良好。它可以创建/显示/更新/删除报告记录。我只是将更新功能复制到员工应用程序,但有上述问题。可能是报告应用程序的模型具有来自员工应用程序模型(employeelist)的同一类的两个外键。这就是这两个应用程序关联的原因,但我只是不知道如何在保持这种关系的同时解决这个问题。我是 Django 新手,欢迎任何建议。
谢谢

员工名单

{% extends 'base.html' %} 
{% block office_block%}
{% if employee_list %}
    <title>Employee List</title>
    <table class = 'table table-striped table-hover' id = 'my_table'>
      <thead>
        <tr>
          <th><button type="button" class = ' btn btn-info' onclick = 'sortTable(0)'>Name</button></th>
          <th><button type="button" class = ' btn btn-info' onclick = 'sortTable(1)'>Phone Ex</button></th>
          <th><button type="button" class = ' btn btn-info' onclick = 'sortTable(2)'>Email</button></th>
          <th><button type="button" class = ' btn btn-info' onclick = 'sortTable(3)'>Department</button></th>
          <th><button type="button" class = ' btn btn-info' onclick = 'sortTable(4)'>Remote Access</button></th>
          <th>Cell Phone</th>
        </tr>
      </thead>
      <tbody>
        {% for EL in employee_list %}
          <tr>
            <td><a href="{% url 'employee:employee_update' pk=EL.id %}">{{EL.first_name}} {{EL.last_name}}</a></td>
            <td>{{EL.phone_ex}}</td>
            <td>{{EL.email}}</td>
            <td>{{EL.department}}</td>
            <td>{{EL.remote_access}}</td>
            <td>{{EL.cell}}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  <!-- </div> -->
{% else %}
  <p class = 'bg-primary'>Employee List not found</p>
{% endif %}
{% endblock %}

urls.py

from django.urls import path
from . import views
app_name = 'employee'
urlpatterns = [
path('output/', views.EmployeeOutput.as_view(), name = 'employee_list'),
path('input/', views.EmployeeInput.as_view(), name = 'employee_input'),
path('update/<int:pk>/', views.EmployeeUpdate.as_view(), name = 'employee_update'),
]

views.py 员工APP

from django.shortcuts import render
from . import models
from employee.forms import EmployeeInputForms
from django.views.generic import (ListView, DetailView, CreateView, UpdateView, DeleteView,)

class EmployeeOutput(ListView):
model = models.EmployeeList
context_object_name = 'employee_list'
template_name = 'employee_list.html'


class EmployeeInput(CreateView):
model = models.EmployeeList
template_name = 'employee_input.html'

form_class = EmployeeInputForms

class EmployeeUpdate(UpdateView):
model = models.EmployeeList
form_class = EmployeeInputForms
context_object_name = 'employee_detail'
template_name = 'employee_detail.html'

base.html

<!DOCTYPE html>
{% load staticfiles %}
{% load bootstrap4 %}       {# import bootstrap4/bootstrap3 #}
{% bootstrap_css %}         {# Embed Bootstrap CSS #}
{% bootstrap_javascript jquery='full' %}  {# Embed Bootstrap JS+jQuery #}

<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="{% static 'css/my_css.css' %}">
</head>
<body>
<div class = 'body_style'>
  <nav class="navbar navbar-expand navbar-light sticky-top" style =     'background-color: #7DBAF2;'>
    <a class="navbar-brand">Marklyn</a>
    <button type="button" class = 'navbar-toggler' data-toggle = 'collapse' data-target = '#navbarSupportedContent' aria-controls = 'navbarSupportedContent' aria-expanded = 'false'
    aria-label  = 'Toggle navigation'> <span class = 'navbar-toggler-icon'></span>
    </button>
    <div class="collapse navbar-collapse" id = 'navbarSupportedContent'>
      <ul class = 'navbar-nav mr-auto'>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'home:home_page' %}"> <button type="button" class = 'btn btn-secondary'>Home</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'admin:index' %}"> <button type="button" class = 'btn btn-secondary'>Admin</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'report:track_report' %}"> <button type="button" class = 'btn btn-secondary'>Report</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'report:track_input' %}"> <button type="button" class = 'btn btn-secondary'>Enter Request</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'report:track_data_export' %}"> <button type="button" class = 'btn btn-secondary'>Export data</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'employee:employee_list' %}"> <button type="button" class = 'btn btn-secondary'>Staff</button><span class = 'sr-only'>(current)</span> </a></li>
        <li class = 'nav-item active'><a class="nav-link" href="{% url 'employee:employee_input' %}"> <button type="button" class = 'btn btn-secondary'>Create Staff</button><span class = 'sr-only'>(current)</span> </a></li>

      </ul>
    </div>

    <ul class = 'navbar-nav ml-auto'>
      <form class="form-inline my-2 my-lg-0" >
        <li><a class="nav-link" href="{% url 'authentication:register' %}"> <button type="button" class = 'btn btn-primary'>Register</button></a></li>
        {% if user.is_authenticated %}
          <li> <a class = 'navbar-link' href="{% url 'authentication:user_logout' %}"><button type="button" class = 'btn btn-success'>Logout</button></a></li>
        {% else %}
          <li> <a class = 'navbar-link' href="{% url 'authentication:user_login' %}"><button type="button" class = 'btn btn-warning'>Login</button></a></li>
        {% endif %}
      </form>
    </ul>
  </nav>
  <div class="bar_on_top">
    {% block office_block %}
    {% endblock %}
  </div>
</div>

<script src = "{% static '/js/sort.js' %}"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>

employee_detail.html

{% extends 'base.html' %}
{% block office_block%}
<title>Request Detail</title>
<h1 class = 'my_header'>
  Update Current Request
</h1> <br>
<form enctype="multipart/form-data" method="post">
  {% csrf_token %}
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-6">
          <table class = 'table table-hover'>
          {{forml}}
          </table>
        </div>
      </div>
      <input type="submit" class = 'btn btn-info' value="Update">
      <!-- <a href="{% url 'report:track_delete' pk=form_delete.pk %}"><button type="button" class = 'btn btn-danger'>Delete</button></a> -->
      <!-- <a href="{% url 'report:track_report'%}"><button type="button" class = 'btn btn-warning'>Cancel</button></a> -->
    </div>
</form>
{% endblock %}

【问题讨论】:

  • line 22 of employee_list.html 说什么?显然,您有一个链接({% url %} 标记)尝试传递 pk 参数,而您的 delete 网址不希望这样。
  • 刚刚注意到您发布了employee_list.html,而错误出现在您的employee_detail.html:GET Request URL: http://127.0.0.1:8000/employee/update/7。请发帖employee_detail.html。
  • 好的,employee_detail.html 已更新。我尝试使用实际的上下文对象名称:employee_detail 而不是“form”。好像没什么区别……
  • 好的。 O 看看问题出在哪里。 Django解析employee_detail.html的注释部分,即使它被注释掉了......我删除了这两个注释行,指向报告,一切正常。谢谢大家的帮助和支持!!
  • 是的,它只对浏览器隐藏。如果您真的将其注释掉,请使用{% comment %} 模板标签。

标签: django python-3.x foreign-keys


【解决方案1】:

发现该问题是 Django 2.1.5 以下版本中的错误,该错误已在 Django 2.1.5 中解决。所以安装 Django 2.1.5 你不会得到任何错误。

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 2012-10-23
    • 1970-01-01
    • 2013-07-25
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    相关资源
    最近更新 更多