Is there a way to alter the request.path before matching the urls?

好久不见. 提交于 2019-12-19 02:27:07

问题


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) + '/')

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


回答1:


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




回答2:


Why do you want to change the url, then match that new url? Why not have the url direct to the view and method you want, then work on the request.user.id like you would had you changed the url?

Perhaps another example would illustrate what you are trying to do.

(Making this an answer since I can't comment)



来源:https://stackoverflow.com/questions/9320693/is-there-a-way-to-alter-the-request-path-before-matching-the-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!