【问题标题】:Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/index.html/找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/index.html/
【发布时间】: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

标签: python django


【解决方案1】:

您需要更新您的success_url

由于您的应用主页定义为:

path('', views.index, name='index')

您可以编辑为:

success_url = "/"

【讨论】:

    【解决方案2】:

    改变这一行

    success_url = '/index.html/'
    

    success_url = 'index' # this is the name of the url path in your urls.py
    

    你也可以改变这一行

    return redirect('todoxd_app/new_todo')
    

    return redirect('new_todo') # again the same thing.... name of the url path in your urls.py file
    

    【讨论】:

    • 你在课堂上没有函数。您的代码不完整
    • 我看到有些人没有使用他们自己的函数,而是来自 django.urls 的 reverse_lazy,但它在我的项目中不起作用
    猜你喜欢
    • 2016-10-30
    • 2019-08-01
    • 2020-08-15
    • 2023-03-22
    • 2018-07-13
    • 2021-07-25
    • 2020-07-11
    • 2021-07-24
    相关资源
    最近更新 更多