Django 使用reverse()重定向到依赖于查询字符串的URL
我正在编写一个 Django 应用程序,其 URL 类似于“http://localhost/entity/id/?overlay=other_id”。其中 id 是特定实体的主键,overlay 是要在显示中覆盖的第二个实体的可选查询参数。用户只能在通过覆盖层查看对象时更新实体。当发布到 /update/id 时,我想重定向回 /entity/id,但我不想在重定向期间丢失我的查询参数,因为视图的更改会令人不快。
例如,我的 url.py 中有以下内容:
...
(r'^update/(?P<id>.+)/(?P<overlay_id>.+)/$', 'update'),
(r'^entity/(?P<id>.+)/$', 'view'),
...
因为更新时需要 override_id,所以它是 URL 的一部分,而不是查询参数。在 django 视图中,我想在成功 POST 后进行重定向,并使用reverse() 来避免在 python 代码中引用 URL。总体思路是:
return HttpResponseRedirect(
reverse('views.view',
kwargs={
'id': id,
},
)
)
但是如何反向传递查询参数呢?
谢谢, 克雷格
I'm writing a django application with a URL like 'http://localhost/entity/id/?overlay=other_id'. Where id is the primary key of the particular entity and overlay is an optional query parameter for a second entity to be overlaid in the display. The user can only ever update an entity when viewing objects through an overlay. When POSTing to /update/id, I want to redirect back to /entity/id, but I don't want to lose my query parameter during the redirect, as the change in view would be jarring.
For example, I've got the following in my url.py:
...
(r'^update/(?P<id>.+)/(?P<overlay_id>.+)/
Because overlay_id is required when updating, it's part of the URL, not a query parameter. In the django view I want to redirect after a successful POST and use reverse() to avoid referencing URLs in my python code. The general idea is:
return HttpResponseRedirect(
reverse('views.view',
kwargs={
'id': id,
},
)
)
But how do I pass my query parameter though reverse?
Thanks,
Craig
, 'update'),
(r'^entity/(?P<id>.+)/
Because overlay_id is required when updating, it's part of the URL, not a query parameter. In the django view I want to redirect after a successful POST and use reverse() to avoid referencing URLs in my python code. The general idea is:
But how do I pass my query parameter though reverse?
Thanks,
Craig
, 'view'),
...
Because overlay_id is required when updating, it's part of the URL, not a query parameter. In the django view I want to redirect after a successful POST and use reverse() to avoid referencing URLs in my python code. The general idea is:
But how do I pass my query parameter though reverse?
Thanks,
Craig
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Django QueryDict 对象:
当然,您可以为其编写一个类似于之前答案的便捷方法。
You can use a Django QueryDict object:
And of course you could write a convenience method for it similar to the previous answer.
您不能只检查
overlay_id
并将其添加到您的网址吗?Can't you just check for an
overlay_id
and add it to your url?查询字符串参数应该正确转义,而不仅仅是连接!
通过字符串连接构建带有查询字符串的 url 与通过字符串连接构建 SQL 查询一样糟糕。它很复杂、不优雅,而且对于用户提供的(不受信任的)输入尤其危险。不幸的是,Django 没有提供一种简单的方法来将查询参数传递到 reverse< /a> 函数。
然而,Python 标准 urllib 提供了所需的查询字符串编码功能。
在我的 应用程序 我创建了一个像这样的辅助函数:
然后我在视图中调用它,如下所示:
请注意空格和感叹号等特殊字符的正确编码!
Query string args should be properly escaped and not just concatenated!
Building an url with query string by string concatenation is as bad idea as building SQL queries by string concatenation. It is complicated, unelegant and especially dangerous with a user provided (untrusted) input. Unfortunately Django does not offer an easy possibility to pass query parameters to the reverse function.
Python standard urllib however provides the desired query string encoding functionality.
In my application I've created a helper function like this:
Then I call it in the view as follows:
Please note the proper encoding of special characters like space and exclamation mark!
您不应该自己生成 url 字符串。给定你的 urls.py 你可以像这样使用反向:
如果你使用 命名 url 模式,非常适合将视图函数与 url 标识符断开连接:
使用反向,如下所示:
You shouldn't generate the url string yourself. Given your urls.py you can use reverse like so:
If you use named url patterns, which are great for disconnecting your view functions from url identifiers:
Use reverse like so: