有没有办法在匹配网址之前更改request.path? [英] Is there a way to alter the request.path before matching the urls?

查看:97
本文介绍了有没有办法在匹配网址之前更改request.path?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我请求包含自我一词的路径时,我想在将其与URL匹配之前用用户ID替换它。我尝试使用这样的中间件:

When I get a request for a path that includes the word 'self' I want to replace it with the user id before matching it to a URL. I tried using a middleware like this:

def process_request(self, request):
    if '/self/' in request.path:
        request.path = request.path.replace('/self/','/' + str(request.user.id) + '/')

替换有效,但显然在URL匹配后完成。

The replacement works but apparently is done after the URL matching. Is there any way to alter the path before this point?

推荐答案

显然,URL行进不是使用<$ c $完成的。 c> request.path ,但 request.path_info 。相同的中间件可以更改此变量。

Apparently, the URL marching is not done using request.path but request.path_info. The same middleware altering this variable works.

这篇关于有没有办法在匹配网址之前更改request.path?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆