【问题标题】:Page not found (404) Request Method: GET找不到页面 (404) 请求方法:GET
【发布时间】:2020-04-06 04:40:20
【问题描述】:

我不断收到此错误:

  • 找不到页面 (404)
  • 请求方法:GET
  • 请求网址:http://127.0.0.1:8000/home 使用personal_portfolio.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

admin/

home/ 当前路径 home 与其中任何一个都不匹配。


这是我整个项目的urls.py

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('home/', include('hello_world.urls')),
]

这是我的hello_world urls.py代码

from django.urls import path
from hello_world import views

urlpatterns = [
    path('home/', views.hello_world, name='hello_world'),
]

这是我的 hello_world views.py 代码

from django.shortcuts import render

def hello_world(request):
    return render(request, 'hello_world.html')

【问题讨论】:

    标签: python django


    【解决方案1】:

    您需要更改此网址

    urlpatterns = [
        path('home/', views.hello_world, name='hello_world'),
    ]
    

    urlpatterns = [
        path('', views.hello_world, name='hello_world'),
    ]
    

    http://127.0.0.1:8000/home/

    否则您需要转到http://127.0.0.1:8000/home/home/ 获取当前网址

    【讨论】:

    • 谢谢,这很有帮助,但为什么我们需要两个 'home/' 才能成为正确的 url?
    猜你喜欢
    • 2016-10-30
    • 2019-08-01
    • 2020-08-15
    • 2023-03-22
    • 2018-07-13
    • 2021-07-25
    • 2021-10-30
    • 2020-07-11
    相关资源
    最近更新 更多