路由重定向
This commit is contained in:
parent
c7aad21e87
commit
7807b60170
@ -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<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/")),
|
||||
]
|
||||
|
||||
10
helloWorld/static/error.html
Normal file
10
helloWorld/static/error.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
系统运行有问题
|
||||
</body>
|
||||
</html>
|
||||
@ -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,6 +9,10 @@ def index(request):
|
||||
|
||||
|
||||
def blog(request, 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):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user