Several redirection methods in Django and several redirection methods in django

Source: Internet
Author: User

Several redirection methods in Django and several redirection methods in django

Here, django1.5 is used.

Requirement: There is interface A, where there is A form B. After the front-end submits B, the data is saved in the background, and interface A is returned. If the Save fails, an error is prompted ON INTERFACE.

Here we need redirection in the background, and the parameter, that is, the error message, must be included.
Several methods are collected here. I will briefly explain how to use those packages.

1. Use HttpResponseRedirect

The first argument to the constructor is required-the path to redirect. this can be a fully qualified URL (e.g. 'http: // www.yahoo.com/search/') or an absolute path with no domain (e.g. '/search /'). The parameter can be either a complete url or an absolute path.
Copy codeThe Code is as follows:
From django. http import HttpResponseRedirect

@ Login_required
Def update_time (request ):
# Pass... form processing
Return HttpResponseRedirect ('/commons/invoice_return/index/') # Jump to the index page

If you want to pass parameters, you can use url parameters
Copy codeThe Code is as follows:
Return HttpResponseRedirect ('/commons/invoice_return/index /? Message = error ') # Jump to the index page

In this way, you can get the error information in the index processing function.

Ii. redirect and reverse

Copy codeThe Code is as follows:
From django. core. urlresolvers import reverse
From django. shortcuts import redirect
# Https://docs.djangoproject.com/en/1.5/topics/http/shortcuts/

@ Login_required
Def update_time (request ):
# Pass... form processing
Return redirect (reverse ('commons. views. invoice_return_index ', args = []) # Jump to the index interface

Redirect is similar to HttpResponseRedirect. You can also use the url format of the string/. inidex /? A = add
Reverse can directly use the views function to specify the redirection handler. args is the url matching value. For more information, see

Iii. Others

Other parameters can also be configured directly in the url, but you do not know how to pass parameters.
Copy codeThe Code is as follows:
From django. views. generic. simple import redirect_to

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

We can even use the session method to pass values.
Copy codeThe Code is as follows:
Request. session ['error _ message'] = 'test'
Redirect ('% s? Error_message = test' % reverse ('page _ Index '))

These methods are similar to location refresh, and the client re-specifies the url.
The method for directly returning response to the client is not found yet.

Study:

Is the previous idea too rigid, redirection? If you need to carry parameters, can you directly call the method corresponding to the url in views to implement it? A parameter is specified by default.
For example, there is a method of baseinfo_account in the view, and another url (corresponding to the view method of blance_account) will be redirected to this baseinfo_account.

Url Configuration:
Copy codeThe Code is as follows:
Urlpatterns = patterns ('',
Url (r '^ baseinfo/', 'account. views. baseinfo_account '),
Url (r '^ blance/', 'account. views. blance_account '),
)

Copy codeThe Code is as follows:
@ Login_required
Def baseinfo_account (request, args = None ):
# It seems inappropriate to write the code according to the normal url.
If args:
Print args
Return render (request, 'accountuserinfo.html ', {"user": user })
 
 
@ Login_required
Def blance_account (request ):
Return baseinfo_account (request, {"name": "orangleliu "})

Test:
1. directly access/baseinfo (test OK)
2. Access/blance to the/baseinfo page and obtain the parameters (test OK. The page is/baseinfo, but the url in the browser's address bar is still/blance)

This method with parameter redirection is feasible.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.