Django - 模拟 http-post 请求
我有这个视图函数search(request)
。网址后缀是/search
。它需要一些 POST 参数并相应地显示搜索结果。
我想创建第二个函数 show_popular(request)
。它不需要发布或获取参数。但它应该使用一些硬编码的后参数来模拟对搜索函数的调用。
我想在不改变任何现有功能中的任何内容并且不改变设置的情况下实现这一目标。这可能吗?
编辑:我知道这可以通过将搜索重构为单独的函数并让多个视图函数调用它来实现。但在这个特殊情况下,我对此不感兴趣。就我而言, show_popular 函数只是临时的,并且出于不相关的原因我不想重构。
I have this view function search(request)
. The url suffix is /search
. It takes a few POST parameters and shows search results accordingly.
I want to make a second function show_popular(request)
. It takes no post or get parameters. But it should emulate a call to the search function with some hard coded post parameters.
I want to achieve this without changing anything in any existing function and without changing setup. Is that possible?
EDIT: I know this can be achieved by refactoring the search into a separate function and have several view functions call this. But in this particular case, I am not interested in that. In my case the show_popular function is only temporary, and for irrelevant reasons I do not wish to re-factor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,但你不想这样做。将
search()
重构为处理请求的函数和执行搜索的函数,并从show_popular()
调用后者。Yes, but you don't want to do that. Refactor
search()
into a function that handles the request and a function that performs the search, and call the latter fromshow_popular()
.