django框架之自定義重定向頁面

操做環境同前一篇django文章。python


url重定向就是,假如瀏覽器地址訪問的是:192.168.255.70/booktest/redirecttest1,通過重定向設置後,瀏覽器url地址重定向到:192.168.255.70/booktest/redirecttest2web


啓動web訪問:django

cd /root/py3/django-test1/test3
python manage.py runserver 192.168.255.70:8000


定義視圖函數:vim

cd /root/py3/django-test1/test3
vim booktest/views.py 

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect

...
def redirectTest1(request):
#redirect()是HttpResponseRedicrect()的一種簡寫方法,就如同render()是HttpResponse()的一種簡寫方法同樣。
#    return HttpResponseRedirect('/booktest/redirectTest2/')
    return redirect('/booktest/redirectTest2')
def redirectTest2(request):
    return HttpResponse('this is redirect page')

配置url路由:瀏覽器

cd /root/py3/django-test1/test3
vim booktest/urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    ...
    url(r'^redirectTest1/$',views.redirectTest1),
    url(r'^redirectTest2/$',views.redirectTest2),
]

瀏覽器測試重定向,訪問:http://192.168.255.70:8000/booktest/redTest1 bash

自動變爲:http://192.168.255.70:8000/booktest/redirectTest2/ ide

QQ截圖20181125024108.png

相關文章
相關標籤/搜索