如何通过django的process_request更改网址。
这是我的代码:
class MobileMiddleware(object):
def process_request(self, request):
if request.path.startswith('/core/mypage/'):
request.path='/core/mypage/?key=value'
print request.path,'aaaa'
我想在页面的地址是 /core/mypage/
时,添加一个参数 key
,
这样浏览器的地址就会变成 http:www.ss.com/core/mypage/?key=value
但是,浏览器的地址并没有改变。
我该怎么做呢?
5 个回答
1
试试这个
return HttpResponseRedirect('/core/mypage/?key=value')
3
问题在于 HttpRequest.path
只是一个普通的属性。改变它并不会给浏览器发出新的指令。你可能想要使用 重定向 方法,这样才能真正让浏览器去其他地方。
17
给搜索的朋友们 - 我测试过使用 request.path_info。如果你想在中间件中修改网址,可以在 process_request 里更改 request.path_info。
request.path_info = <change request.path_info>
请注意,我并不是建议你使用这个,也不是说不可以用。我只是告诉你,如果你想修改网址,这就是你可以采用的方法。