从HTML模板到URL的URL中出现错误。没有反向匹配

时间:2018-10-11 09:01:09

标签: django django-views django-urls

我试图基于主键删除记录,所以我试图将PK传递给url进入视图功能,但出现此错误:

django.urls.exceptions.NoReverseMatch:

  <form method="POST" action="{% url 'essay:delete_view' teacher.id %}">
     <div class="page-header">
      <div class="span4">
        <h1>Manage Essays</h1>
      </div>
  </div>
    <table>
        <tr>
            <th>Title</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
      {% csrf_token %}
        {% for uploadassignment in teacher.uploadassignment_set.all %}
         <tr>
             <th for="uploadassignment{{ forloop.counter }}">{{uploadassignment.title}}</th>
             <th><input  type="submit" value="Edit" class="btn btn-primary"/></th>
             <th><input type="submit" value="Delete" name="delEssay" id="delEssay" class="btn btn-primary"/></th>
         </tr>
        {% endfor %}
    </table>
        </form>

我的html代码:

      url(r'^(?P<essay_id>[0-9]+)/delEssay/$', views.delete_view, name='delete_view')

URL:

def delete_view(request,essay_id):
 print("Delete")
 print(essay_id)
 object = UploadAssignment.objects.get(id=essay_id)
 object.delete()
 return render(request, 'essay/THome.html')

查看功能:

Table1
+----------+----------------+-----------+---------+----------+
| commonid | creation_date  | column 1  | column 2| column 3 |
+----------+----------------+-----------+---------+----------+

Table2
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+

Table3
+----------+---------+----------+---------+----------+
| commonid | column 1| column 2 | column 3| column 4 |
+----------+---------+----------+---------+----------+

1 个答案:

答案 0 :(得分:0)

您正在尝试访问对象ID teacher.id,而是尝试使用主键pk,例如teacher.pk

我遇到了同样的问题,我用pk替换了它,并且奏效了。

希望这可以解决您的问题。