Is there a way to change request.path before matching URLs?

When I get a request for a path that includes the word "I", I want to replace it with the user ID before matching it with the URL. I tried using middleware as follows:

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

Replacement works, but apparently does after matching the URL. Is there any way to change the path to this point?

+4
source share
2 answers

Apparently URL mashing is not done using request.path , but request.path_info . The same middleware that modifies this variable works.

+6
source

Why do you want to change the URL and then map this new URL? Why don't you give the url the kind and method you want and then work with request.user.id as if you changed the url?

Perhaps another example illustrates what you are trying to do.

(Make this an answer as I cannot comment)

+1
source

Source: https://habr.com/ru/post/1396948/


All Articles