Change Address in Django Middleware

I do not know if this is possible, but I would like to add a few parameters at the end of the URL using middleware. Can this be done without redirecting after requesting a fix to the url?

i.e. user clicks: ... / some_link and middleware overwrites it: ... / some_link? par1 = 1 & par2 = 2

Another way is to change the response and replace each HTML link, but that is not what I would like to do.

thanks

+3
source share
3 answers

I think it really depends on your problem and what exactly you are trying to do.

URL- , URL- . - , , URL. , JavaScript, , , .

, , , URL-. ?

, , URL.

+1
class YourRedirectMiddleware:

    def process_request(self, request):
        redirect_url = request.path+'?par1=1&par2=2'
        return HttpResponsePermanentRedirect(redirect_url)

?

+4

You can do whatever you want with middleware. You have access to the request object, you can get the URL and redirect to a new one if you want.

My question will be, why do you want to do this? If you need to save request information, a suitable place for this would be in the session.

0
source

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


All Articles