将 Django 模板字典转发到另一个模板?
是否可以在另一个视图中重用模板字典?
例如,假设视图在数据库上执行搜索并将字典中的搜索结果提供给模板。该模板显示前 10 个结果,并具有一个链接以在另一个页面中显示所有结果。
是否可以转发包含搜索结果的模板字典以避免再次执行相同的搜索?
Is it possible to reuse a template dictionary in another view ?
For instance, imagine a view performs a search on the db and provide the search results in a dictionary to a template. The template displays the first 10 results and has a link to display all the results in another page.
Is it possible to forward the template dictionary containing the search results to avoid having to perform the same search again ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并不真地。您无法跨页面视图保留任何内容 - 除非将其存储在某个地方,例如在会话中。您无法将其放入模板本身中,因为接下来需要通过 POST 将其发送回服务器以进行下一个请求。
无论如何,没有太大必要这样做。如果您使用内置的 Paginator 类对搜索进行分页,Django 将在查询中自动使用 LIMIT 和 OFFSET,以便只查询您实际显示的对象。
Not really. You can't preserve anything across page views - except by storing it somewhere, eg in the session. You couldn't put it in the template itself, as that would then need to be sent back to the server via a POST for the next request.
In any case, there's not much need to do this. If you use the built-in Paginator class to paginate your search, Django will automatically use LIMIT and OFFSET in the query so that only the objects you're actually displaying will be queried.