上周我开始使用Python和Django进行编码,现在我开始对此感到绝望。 我必须处理待办事项列表,并希望通过单击任务旁边的按钮来删除ToDo任务。 现在我总是得到一个
NoReverseMatch at / delete / 1
错误:(
delete_confirm.html:
{% extends "base_page.html" %}
{% block title %}Confirm Todo delete{% endblock %}
{% block content %}
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to delete "{{ object }}"?</p>
<input type="submit" value="Confirm" />
</form>
{% endblock %}
我的Urls.py:
from django.conf.urls import patterns, url
from todolist import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^add', views.AddView.as_view(), name='add'),
url(r'^impressum', views.ImpressumView.as_view(), name='impressum'),
url(r'^edit/(?P<pk>\d+)$', views.UpdateView.as_view(), name='todo_edit'),
url(r'^delete/(?P<pk>\d+)', views.DeleteView.as_view(), name='todo_delete'),
)
views.py:
from django.shortcuts import render
from django.core.urlresolvers import reverse_lazy
from django.http import HttpResponse
from django.views import generic
from django.views.generic import TemplateView, ListView, CreateView, UpdateView, DeleteView
from todolist.models import Todo
# Create your views here.
class IndexView(ListView):
template_name = 'index.html'
model = Todo
class ImpressumView(TemplateView):
template_name = 'impressum.html'
class AddView(CreateView):
template_name = 'add.html'
model = Todo
fields = ['title','deadline','progress']
success_url = '/'
class UpdateView(UpdateView):
template_name = 'edit.html'
model = Todo
fields = ['title','deadline','progress']
success_url = '/'
class DeleteView(DeleteView):
template_name = 'delete_confirm.html'
model = Todo
success_url = reverse_lazy('/')
index.html的有趣部分:
<!--Table content-->
{% for todo in object_list %}
<tr>
<td class="text-left">{{todo.title}}</td>
<td> {{todo.deadline}}</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="{{todo.progress}}" aria-valuemin="0" aria-valuemax="100" style="width: {{todo.progress}}%">
{{todo.progress}}%
</div>
</div>
</td>
<td >
<div class="btn-group">
<a href="edit/{{todo.id}}" role="button" class="btn btn-default">Edit</a>
<a href="delete/{{todo.id}}" role="button" class="btn btn-default">Delete</a>
<button class="btn btn-default" data-toggle="tooltip" title="Done?" data-placement="right"><span class="glyphicon glyphicon-check"></button>
</div>
</td>
{% endfor %}
如果你的一些人可以帮助一个新手,那会很棒:)
答案 0 :(得分:3)
问题在于删除视图中的这一行。
class DeleteView(DeleteView):
...
success_url = reverse_lazy('/')
您可以提供网址:
success_url = '/'
或将reverse_lazy
与您要撤消的网址名称一起使用:
success_url = reverse_lazy('index')