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.
后来发现是忘记把应用(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)
]