修改Django中间件中的地址
我不知道是否可行,但我想使用中间件在 URL 末尾添加一些参数。 modyf 请求的 URL 后可以不重定向来完成吗?
IE。 用户点击:.../some_link 中间件将其重写为: .../some_link?par1=1&par2=2
其他方法是修改响应并替换每个 HTML 链接,但这不是我想要做的事情。
谢谢
I don't know if it's possible but I'd like to add few parameters at the end of the URL using middleware. Can it be done without redirect after modyfing requested URL?
ie.
user clicks: .../some_link
and middleware rewrites it to: .../some_link?par1=1&par2=2
Other way is to modify reponse and replace every HTML link but it's not something I'd like to do.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你想实现什么目标?为什么要这样做?
what are you trying to accomplish and why this way?
我认为这实际上取决于您的问题以及您到底想做什么。
您无法在不重定向用户的情况下更改 URL,因为您无法在不重新加载的情况下修改页面上的 URL。基本上,重定向是告诉用户继续前进的响应,无法实际更改 URL。请注意,即使您使用 JavaScript 之类的方式执行此操作,您基本上也与重定向相同,因此无法在客户端或服务器端完成。
我认为如果您向我们解释为什么需要通过 URL 传递此信息可能会有所帮助。为什么不在会话中存储数据?
我想您可以将数据添加到请求对象,但这不会将其添加到 URL。
I think this really depends on your problem and what exactly you are trying to do.
You cannot change the URL without redirecting the user, as you cannot modify the URL on a page without a reload. Basically a redirect is a response telling the user to move on, there is no way to actually change the URL. Note that even if you do it in something like JavaScript you basically do the same as a redirect, so it can't be done client or server side.
I think it might help if you explain to us why you need to pass this information via the URL. Why not store data in the session?
I guess you could add the data to the request object but that doesn't add it to the URL.
您可以在中间件中做任何您喜欢的事情。您有权访问请求对象,如果需要,您可以获取 URL 并重定向到新的 URL。
我的问题是,你为什么要这样做?如果您需要保留有关请求的信息,则正确的位置是在会话中。
You can do whatever you like in the middleware. You have access to the request object, you can get the URL and redirect to a new one if you want.
My question would be, why do you want to do this? If you need to keep information about the request, the proper place to do this is in the session.