找不到带有关键字参数的Django NoReverseMatch

时间:2018-09-18 04:54:09

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

感谢您的帮助。长仓短,这是我的git repo。 https://github.com/nelsonyan/track 单击employee_list.html的第22行的href时,出现以下错误消息。应该将我带到employee_detail.html进行更新,但是给了我关于track_delete URL标记的以下错误信息...这两个应用程序(报告,员工)具有关联的外键,但是我没有了解为什么员工应用程序的更新功能与报表应用程序的删除功能相关。 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的新手,欢迎任何建议。
谢谢

employee_list

{% 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 %}

1 个答案:

答案 0 :(得分:0)

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