Several redirection methods in Django

Source: Internet
Author: User

This article mainly introduces several kinds of Django methods, this article explains the use of httpresponseredirect, redirect, reverse and configuration files in the configuration URL, and other methods, the need for friends can refer to the

This is django1.5.

Requirements: There is an interface a, which has a form B, after the foreground to submit B, after the background to save the data, return interface A, if the save failure needs to be prompted in the A interface error.

This requires a background redirection, and you need to be able to take parameters, which is the error message

There are several methods collected here, Jane replied that the need for those bags, how simple to use.

First, the use of Httpresponseredirect

The the argument to the constructor are required–the path to redirect to. This can is a fully qualified URL (e.g. '') or a absolute path with no domain (e.g. '/search/') )。 Parameter can be either a full URL or an absolute path.

The code is as follows:

From django.http import Httpresponseredirect


def update_time (Request):

#pass ... form processing

Return Httpresponseredirect ('/commons/invoice_return/index/') #跳转到index界面

If you need to pass parameters, you can pass the URL parameter

The code is as follows:

Return Httpresponseredirect ('/commons/invoice_return/index/?message=error ') #跳转到index界面

This allows you to get to the error message in the index handler function.

Ii. Redirect and reverse

The code is as follows:

From django.core.urlresolvers Import Reverse

From django.shortcuts Import redirect



def update_time (Request):

#pass ... form processing

Return Redirect (reverse (' Commons.views.invoice_return_index ', args=[)) #跳转到index界面

redirect similar to httpresponseredirect usage, you can also use the URL format of the string/... Inidex/?a=add

Reverse can use the views function directly to specify the redirected handler function, args is the URL matching value. See documentation for details

Third, other

Other can also be configured directly in the URL, but do not know how to pass the parameters.

The code is as follows:

From django.views.generic.simple import redirect_to

Add (R ' ^one/$ ', redirect_to, {' URL ': '/another/'}) in the URL,

We can even use the session method to pass values

Copy code code as follows:

request.session[' error_message ' = ' test '

Redirect ('%s?error_message=test '% reverse (' page_index '))

These methods are similar to location refreshes, and the client assigns URLs again.

Haven't found how to jump the handler function on the server side, directly return the method response to the client.

2014-11-13 Study:

is not before the idea too rigid, redirect, if you need to carry parameters, then can directly call the views in the URL corresponding method to implement it, the default to specify a parameter.

For example, there is a method baseinfo_account in view, and then another URL (corresponding to the View method Blance_account) is redirected to this baseinfo_account.

Configuration in URL:

Copy code code as follows:

Urlpatterns = Patterns ("",

URL (r ' ^baseinfo/', ' Account.views.baseinfo_account '),

URL (r ' ^blance/', ' Account.views.blance_account '),


Copy code code as follows:


def baseinfo_account (Request, Args=none):

#按照正常的url匹配这么写有点不合适, it doesn't look normal.

If args:

Print args

return render (Request, ' accountuserinfo.html ', {"User": User})


def blance_account (Request):

return Baseinfo_account (Request, {"name": "Orangleliu"})

You need to test for:

1 Direct Access/baseinfo is normal (test OK)

2 Access/blance can be normal redirect to the/baseinfo page, and get to the parameters (test OK, the page is/baseinfo but the URL of the browser address bar is still/blance)

This kind of parameter redirection is feasible.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.