1.有可能在配置urls会遇到这个问题
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in djangoProject.urls, Django tried these URL patterns, in this order:
students/ [name='students']
teachers/ [name='teachers']
guest/ [name='guest']
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Django尝试匹配URL时发现空路径(http://127.0.0.1:8000/
)没有匹配到任何定义的URL模式。
这个问题可能是由于以下原因之一导致的:
你可能没有在urls.py
文件中定义空路径的URL模式。请确保你的urls.py
文件中包含了一个空路径的URL模式,例如:
from django.urls import path
from .views import student_view, teacher_view, guest_view
urlpatterns = [
path('', student_view, name='home'),
# 其他URL模式...
]
我的项目是这样的
from django.urls import path
from .views import student_view, teacher_view, guest_view
app_name = 'AISP' # 替换为你的应用程序名称
urlpatterns = [
path('', student_view, name='home'), # 添加空路径的URL模式
path('students/', student_view, name='students'),
path('teachers/', teacher_view, name='teachers'),
path('guest/', guest_view, name='guest'),
]
这个分别对应三个页面 students teachers guest 页面,返回的内容在view.py中定义,可以返回HTML页面
下面是views.py的代码
from django.http import HttpResponse
# Create your views here.
def student_view(request):
# 处理学生页面的逻辑
return HttpResponse("This is the student page.")
def teacher_view(request):
# 处理教师页面的逻辑
return HttpResponse("This is the teacher page.")
def guest_view(request):
# 处理访客页面的逻辑
return HttpResponse("This is the guest page.")
要是想返回相应的页面,那就定义好html页面在template里!