Django 简明教程
Django - Page Redirection
在网络应用程序中,页面重定向有诸多原因。当操作发生或者出错时,你可能希望将用户重定向到其他页面。例如,当用户登录到你的网站时,他经常被定向到主页或者他的个人信息中心。在 Django 中,重定向使用“重定向”方法实现。
Page redirection is needed for many reasons in web application. You might want to redirect a user to another page when a specific action occurs, or basically in case of error. For example, when a user logs in to your website, he is often redirected either to the main home page or to his personal dashboard. In Django, redirection is accomplished using the 'redirect' method.
“重定向”方法采用以下参数:您希望重定向到的网址字符串。视图名称。
The 'redirect' method takes as argument: The URL you want to be redirected to as string A view’s name.
到目前为止,myapp/views 如下所示 -
The myapp/views looks like the following so far −
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 将更改为 -
Let’s change the hello view to redirect to djangoproject.com and our viewArticle to redirect to our internal '/myapp/articles'. To do so the myapp/view.py will change to −
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 官方网站,我们只需将完整的网址作为字符串传递给“重定向”方法,对于第二个示例(viewArticle 视图,“重定向”方法采用视图名称和参数作为参数。
In the above example, first we imported redirect from django.shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.
访问 /myapp/hello,会看到以下屏幕 -
Accessing /myapp/hello, will give you the following screen −
访问 /myapp/article/42,会看到以下屏幕 -
And accessing /myapp/article/42, will give you the following screen −
还可以通过添加 permanent = True 参数指定“重定向”是暂时的还是永久的。用户不会看到任何区别,但搜索引擎会考虑这些细节来对您的网站进行排名。
It is also possible to specify whether the 'redirect' is temporary or permanent by adding permanent = True parameter. The user will see no difference, but these are details that search engines take into account when ranking of your website.
在映射网址时还能记得到 url.py 中定义的“名称”参数 -
Also remember that 'name' parameter we defined in our url.py while mapping the URLs −
url(r'^articles/(?P\d{2})/(?P\d{4})/', 'viewArticles', name = 'articles'),
该名称(此处为 article)可以用作“重定向”方法的参数,然后我们的 viewArticle 重定向可以从 -
That name (here article) can be used as argument for the 'redirect' method, then our viewArticle redirection can be changed from −
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")
{s0} -
To −
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")
{s1} - 还有一个函数来生成网址;它的用法与 redirect 相同;“反向”方法 (django.core.urlresolvers.reverse)。此方法不会返回 HttpResponseRedirect 对象,而只会返回一个包含与任何传递的参数一起编译的视图网址的字符串。
Note − There is also a function to generate URLs; it is used in the same way as redirect; the 'reverse' method (django.core.urlresolvers.reverse). This function does not return a HttpResponseRedirect object, but simply a string containing the URL to the view compiled with any passed argument.