Page not found (404) - Using the URLconf defined in django01.urls, Django tried these URL patterns,

这篇博客介绍了在遇到Django 404错误时,如何通过检查URLconf和应用URL绑定来解决问题。具体表现为Django尝试的URL模式不匹配当前路径。解决方案是确保在根目录URL配置中正确包含应用的URL配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Page not found (404) - Using the URLconf defined in django01.urls, Django tried these URL patterns, in this order:

admin/
The current path, cong/20, didn’t match any of these.

找不到current path

后来发现是忘记把应用(app)的URL和根目录的URL绑定在一起!

根目录下代码

from django.contrib import admin
from django.urls import path, include  # 为了将我们的应用的URL和根目录URL绑定,要使用include
# from app.views import index
from app import urls as app_urls

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

然后就好啦!
正常了
附上app 中view.py代码:

from django.http import HttpResponse

def index(request, name, age):
    
    return HttpResponse("hello I am {0}, age is {1}".format(name, age))

app urls.py代码

from django.urls import path
# from django.conf.urls import url  # 2.0之前的方法,现在使用会报错
from app.views import index

urlpatterns = [
    path("<str:name>/<int:age>", index)
]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值