首页 > 解决方案 > 没有找到带有关键字参数的 Django NoReverseMatch

问题描述

感谢您的帮助。长存储短,这是我的 git repo。https://github.com/nelsonyan/track 我在单击第 22 行的 href 时收到以下错误消息employee_list.html。它应该带我去employee_detail.html更新,但给了我关于track_deleteURL 标签的以下错误......这两个应用程序(报告,员工)有外键关联,但我不明白为什么员工应用程序的更新功能是相关的到报表应用的删除功能。任何 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 %}

网址.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 %}

标签: djangopython-3.xforeign-keys

解决方案


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


推荐阅读