简体   繁体   English

如何在不重新加载页面的情况下更新 Django 页面?

[英]how to update a Django page without a page reload?

My Django app displays data from a database.我的 Django 应用程序显示来自数据库的数据。 This data changes without user intervention, ie behind the scenes.该数据无需用户干预即可更改,即在幕后。 Whenever it changes, I would like the webpage to update the changed sections without a full page reload.每当它发生变化时,我希望网页更新更改的部分,而无需重新加载整页。

Obviously AJAX springs to mind.显然 AJAX 浮现在脑海中。 When the page is loaded initially (or manually, fully re-loaded later on), the rendered template loads a JavaScript that runs window.onload = update("all"), update(...) in turn triggers a number of XMLHTTPRequests which again return data that gets transformed into HTML pieces for the corresponding sections.当页面最初加载(或手动,稍后完全重新加载)时,呈现的模板会加载运行 window.onload = update("all"), update(...) 的 JavaScript,进而触发许多 XMLHTTPRequests它再次返回被转换为相应部分的 HTML 片段的数据。 All works fine.一切正常。 At the initial page load.在初始页面加载时。

Now I find myself in a Python function that saves a new object to the database.现在我发现自己处于一个将新对象保存到数据库的 Python 函数中。

How do I tell the browser to run update(...) ?如何告诉浏览器运行 update(...) ?

Do I need to somehow manually issue a request to a url that is mapped to a view which in turn renders a template that contains the JavaScript code to run update(...) ???我是否需要以某种方式手动向映射到视图的 url 发出请求,该视图又会呈现一个包含 JavaScript 代码的模板来运行 update(...) ??? Oh my!天啊!

I feel like I'm not following the usual approaches.我觉得我没有遵循通常的方法。 Maybe I'm just standing to close in front of the problem.也许我只是站在问题面前。

Can anyone help me ?谁能帮我 ?

2021 update: Use channels: https://channels.readthedocs.io/en/latest/ 2021 更新:使用频道: https : //channels.readthedocs.io/en/latest/

You have two choices你有两个选择

  1. Have the browser poll using setTimeout()使用 setTimeout() 进行浏览器轮询
  2. Look into Comet -- this is a technique for pushing data from the server to the browser.看看 Comet——这是一种将数据从服务器推送到浏览器的技术。

Here's an article on Comet in Django这是一篇关于Django 中彗星的文章

two approaches:两种方法:

  1. just update the database and wait until the next AJAX query.只需更新数据库并等待下一个 AJAX 查询。 That means it should do the query periodically, you'll have to balance between immediacy and server load.这意味着它应该定期执行查询,您必须在即时性和服务器负载之间取得平衡。 It helps a little if you can do a cheap query to just verify if there has been an update.如果您可以做一个廉价的查询来验证是否有更新,它会有所帮助。 Maybe make that check rely only on memcached instead of going to the DB也许让检查只依赖于 memcached 而不是去数据库

  2. use comet .使用彗星 In short: the client does an AJAX query asking for the update.简而言之:客户端执行 AJAX 查询请求更新。 the server sees there's no update, so it doesn't answer.服务器看到没有更新,所以它不回答。 Instead, the connection is kept open for a long time.相反,连接会长时间保持打开状态。 Eventually either the update comes and the server finally answers, or the client times out and kill the connection.最终要么更新来了,服务器最终回答,要么客户端超时并终止连接。 In that case, the client should immediately reissue the query to keep waiting for the update.在这种情况下,客户端应立即重新发出查询以继续等待更新。

You can also use The Websocket APIhttps://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API .您还可以使用 Websocket APIhttps://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.使用此 API,您可以向服务器发送消息并接收事件驱动的响应,而无需轮询服务器以获取回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM