
- Django 基本概念
- Django - 首頁
- Django - 基礎
- Django - 概述
- Django - 環境搭建
- Django - 建立專案
- Django - 應用生命週期
- Django - 建立檢視
- Django - URL 對映
- Django - 首頁
- Django - 模板系統
- Django - MVT
- Django - 新增主模板
- Django Admin
- Django Admin - 介面
- Django Admin - 建立使用者
- Django Admin - 包含模型
- Django Admin - 設定顯示欄位
- Django Admin - 更新物件
- Django 模型
- Django - 模型
- Django - 插入資料
- Django - 更新資料
- Django - 刪除資料
- Django - 更新模型
- Django 靜態檔案
- Django - 新增靜態檔案
- Django - 新增 CSS 檔案
- Django 高階
- Django - 404 頁面未找到
- Django - 頁面重定向
- Django - 傳送郵件
- Django - 通用檢視
- Django - 表單處理
- Django - 檔案上傳
- Django - Apache 配置
- Django - Cookie 處理
- Django - Session
- Django - 快取
- Django - 評論
- Django - RSS
- Django - AJAX
- Django 資源推薦
- Django - 快速指南
- Django - 資源推薦
- Django - 討論
Django - 頁面重定向
網頁應用中出於多種原因需要頁面重定向。當發生特定操作時,或者在出現錯誤的情況下,您可能需要將使用者重定向到另一個頁面。例如,當用戶登入您的網站時,通常會將其重定向到主頁或其個人儀表盤。在 Django 中,重定向是使用 `redirect` 方法完成的。
`redirect` 方法的引數:要重定向到的 URL(字串) 檢視名稱。
到目前為止,myapp/views 看起來如下:
def hello(request): today = datetime.datetime.now().date() daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] return render(request, "hello.html", {"today" : today, "days_of_week" : daysOfWeek}) def viewArticle(request, articleId): """ A view that display an article based on his ID""" text = "Displaying article Number : %s" %articleId return HttpResponse(text) def viewArticles(request, year, month): text = "Displaying articles of : %s/%s"%(year, month) return HttpResponse(text)
讓我們更改 hello 檢視以重定向到 djangoproject.com,並將我們的 viewArticle 重定向到內部的 '/myapp/articles'。為此,myapp/view.py 將更改為:
from django.shortcuts import render, redirect from django.http import HttpResponse import datetime # Create your views here. def hello(request): today = datetime.datetime.now().date() daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] return redirect("https://www.djangoproject.com") def viewArticle(request, articleId): """ A view that display an article based on his ID""" text = "Displaying article Number : %s" %articleId return redirect(viewArticles, year = "2045", month = "02") def viewArticles(request, year, month): text = "Displaying articles of : %s/%s"%(year, month) return HttpResponse(text)
在上面的例子中,我們首先從 django.shortcuts 匯入 redirect,對於重定向到 Django 官方網站,我們只需將完整的 URL 作為字串傳遞給 `redirect` 方法;對於第二個例子(viewArticle 檢視),`redirect` 方法將檢視名稱及其引數作為引數。
訪問 /myapp/hello,將顯示以下螢幕:

訪問 /myapp/article/42,將顯示以下螢幕:

還可以透過新增 `permanent = True` 引數來指定重定向是臨時的還是永久的。使用者不會看到任何區別,但這對於搜尋引擎在對您的網站進行排名時會考慮到的細節。
還要記住我們在 url.py 中對映 URL 時定義的 `name` 引數:
url(r'^articles/(?P\d{2})/(?P\d{4})/', 'viewArticles', name = 'articles'),
該名稱(此處為 article)可用作 `redirect` 方法的引數,然後我們的 viewArticle 重定向可以從:
def viewArticle(request, articleId): """ A view that display an article based on his ID""" text = "Displaying article Number : %s" %articleId return redirect(viewArticles, year = "2045", month = "02")
更改為:
def viewArticle(request, articleId): """ A view that display an article based on his ID""" text = "Displaying article Number : %s" %articleId return redirect(articles, year = "2045", month = "02")
注意:還有一個函式可以生成 URL;它的使用方法與 redirect 相同;`reverse` 方法(django.core.urlresolvers.reverse)。此函式不返回 HttpResponseRedirect 物件,而只是返回一個包含已編譯檢視和任何傳遞引數的 URL 的字串。