我正在学习有关使用django创建基本网站的youtube教程,编码时出现此错误:
VARCHAR
您看到此错误,因为Django设置文件中的DEBUG = True。将其更改为False,Django将显示标准的404页面。
这是我的代码:
对于urls.py/mysite:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
app/
admin/
The empty path didn't match any of these.
对于views.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
对于urls.py/myapp:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
任何帮助将不胜感激。
答案 0 :(得分:1)
您需要添加根级别路径以访问您指定的路径 http://127.0.0.1:8000/ :
urls.py/mysite
urlpatterns = [
path('', views.index, name='home'),
path('app/', include('myapp.urls')),
path('admin/', admin.site.urls),
]
views.py/mysite
def index(request):
return HttpResponse('This is home page')
答案 1 :(得分:1)
自您指定以来:
path('app/', include('myapp.urls')),
这意味着myapp.urls
中的所有路径都以app/
为前缀。因此,您可以使用http://127.0.0.1:8000/app/
访问索引视图。或者,如果您想使用index
访问http://127.0.0.1:8000/
视图,则将路径重写为:
path('', include('myapp.urls')),