diff --git a/Django5/urls.py b/Django5/urls.py index 28a489f..1c49228 100644 --- a/Django5/urls.py +++ b/Django5/urls.py @@ -17,6 +17,7 @@ Including another URLconf from django.conf import settings from django.contrib import admin from django.urls import path, re_path +from django.views.generic import RedirectView from django.views.static import serve import helloWorld.views @@ -44,4 +45,8 @@ urlpatterns = [ # 媒体路由 re_path('media/(?P.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), + + # 路由重定向,用RedirectVIew实现 + # 访问http://localhost:8000/redirectTo直接跳转到http://localhost:8000/index/ + path('redirectTo',RedirectView.as_view(url="index/")), ] diff --git a/helloWorld/static/error.html b/helloWorld/static/error.html new file mode 100644 index 0000000..fb608ba --- /dev/null +++ b/helloWorld/static/error.html @@ -0,0 +1,10 @@ + + + + + Title + + +系统运行有问题 + + \ No newline at end of file diff --git a/helloWorld/views.py b/helloWorld/views.py index 0003a91..819cc6f 100644 --- a/helloWorld/views.py +++ b/helloWorld/views.py @@ -1,5 +1,5 @@ from django.http import HttpResponse -from django.shortcuts import render +from django.shortcuts import render, redirect # Create your views here. @@ -9,7 +9,11 @@ def index(request): def blog(request, id): - return HttpResponse("id是" + str(id) + "的博客页面") + #输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html + if id==0: + return redirect("/static/error.html") + else: + return HttpResponse("id是" + str(id) + "的博客页面") def blog2(request, year, month, day, id): return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")