【发布时间】:2021-10-30 01:26:31
【问题描述】:
我正在尝试创建简单的待办事项列表。我发现了一个小问题。在删除 todo 任务期间,我收到错误(来自主题),但任务实际上正在删除。如何摆脱这个错误?
urls.py
from django.urls import path, include
from .views import DeleteTodo
from . import views
app_name = 'todoxd_app'
urlpatterns = [
path('', views.index, name='index'),
path('todo/', views.todo, name='todo'),
path('new_todo/', views.new_todo, name='new_todo'),
path('delete/<int:pk>/', DeleteTodo.as_view() ,name='delete_todo'),
]
delete_todo.html
标题 {% 扩展 'todoxd_app/base.html' %} {% 块内容 %} {% csrf_token %} 您确定要删除此任务吗? 删除任务 {% endblock 内容 %}views.py
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from .models import Task
from .forms import TaskForm
from django.views.generic import DeleteView
def index(request):
return render(request, 'todoxd_app/index.html')
def todo(request):
objekt = Task.objects.all()
context = {'objekt': objekt}
return render(request, 'todoxd_app/todo.html', context)
def new_todo(request):
if request.method != 'POST':
form = TaskForm()
else:
form = TaskForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('todoxd_app/new_todo')
context = {'form': form}
return render(request, 'todoxd_app/new_todo.html', context)
class DeleteTodo(DeleteView):
model = Task
template_name = 'todoxd_app/delete_todo.html'
success_url = '/index.html/'
来自 django 项目的网址
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('todoxd_app.urls')),
path('users/', include('users.urls')),
]
如果您发现其他问题,请给我反馈,我只是一个初学者。谢谢!
【问题讨论】:
-
你能给你看看你的 django 项目中的 urls.py 文件吗?
-
/index.html/不是您的 urls.py 中的路由?只需将 success_url 设置为/? -
您在删除记录后重定向到
/index.html/(参见DeleteTodo.success_url),但在您的urls.py中找不到/index.html/ -
应该是
localhost:8000没有index.html