一、 使用HttpResponseRedirect ● fuhao The first argument to the constructor is required – the path to redirect to. This can be a fully qualified URL or an absolute path with no domain。”参数可以是绝对路径跟相对路径”
from django.http import HttpResponseRedirect
@login_required
def update_time (request) :
return HttpResponseRedirect('/' )
return HttpResponseRedirect('/commons/index/?message=error' )
二 redirect和reverse
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
@login_required
def update_time (request) :
return redirect(reverse('test.views.invoice_return_index' , args=[]))
redirect 类似HttpResponseRedirect的用法,也可以使用 字符串的url格式 /..index/?a=add
reverse 可以直接用views函数来指定重定向的处理函数,args是url匹配的值。
三、 其他
from django.views.generic.simple import redirect_to
在url中添加 (r'^test/$' , redirect_to, {'url' : '/author/' }),
request.session['error_message' ] = 'test'
redirect('%s?error_message=test' % reverse('page_index' ))
四
urlpatterns = patterns('' ,
url(r'^index/' , 'account.views.index_account' ),
url(r'^blance/' , 'account.views.blance_account' ),
)
@login_required
def index_account (request, args=None) :
if args:
print args
return render(request, 'accountuserinfo.html' , {"user" : user})
@login_required
def blance_account (request) :
return index_account(request, {"name" : "orangleliu" })