路由重定向

This commit is contained in:
mxjdi 2025-04-26 23:15:30 +08:00
parent c7aad21e87
commit 7807b60170
3 changed files with 21 additions and 2 deletions

View File

@ -17,6 +17,7 @@ Including another URLconf
from django.conf import settings from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.urls import path, re_path from django.urls import path, re_path
from django.views.generic import RedirectView
from django.views.static import serve from django.views.static import serve
import helloWorld.views import helloWorld.views
@ -44,4 +45,8 @@ urlpatterns = [
# 媒体路由 # 媒体路由
re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), re_path('media/(?P<path>.*)', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
# 路由重定向用RedirectVIew实现
# 访问http://localhost:8000/redirectTo直接跳转到http://localhost:8000/index/
path('redirectTo',RedirectView.as_view(url="index/")),
] ]

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
系统运行有问题
</body>
</html>

View File

@ -1,5 +1,5 @@
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render from django.shortcuts import render, redirect
# Create your views here. # Create your views here.
@ -9,7 +9,11 @@ def index(request):
def blog(request, id): 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): def blog2(request, year, month, day, id):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面") return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")