如何更新没有页面重新加载的Django页面? [英] how to update a Django page without a page reload?

查看:125
本文介绍了如何更新没有页面重新加载的Django页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Django应用程序显示数据库中的数据。该数据在没有用户干预的情况下,即幕后改变。每当更改时,我希望网页更新更改的部分,而不需要重新整理页面。



显然,AJAX非常注意。当页面最初加载(或手动,完全重新加载)后,渲染的模板加载运行window.onload = update(all)的JavaScript,update(...)反过来触发了一些XMLHTTPRequests它再次返回被转换为相应部分的HTML片段的数据。一切工作正常在初始页面加载。



现在我发现自己在一个Python函数中,将一个新对象保存到数据库。



如何告诉浏览器运行update(...)?



我需要以某种方式手动发出一个映射到视图反过来渲染包含JavaScript代码的模板来运行update(...)???哦,我的!



我觉得我没有遵循惯例。
也许我只是站在这个问题前面关闭。



任何人都可以帮助我吗?

解决方案

两种方法:


  1. 只需更新数据库,等到下一个AJAX查询这意味着它应该定期执行查询,您必须在即时性和服务器负载之间取得平衡。它有助于一点点,如果你可以做一个便宜的查询,以验证是否有更新。可能使该支票仅依赖于memcached而不是去DB。




    • 使用彗星。简而言之:客户端执行一个AJAX查询,要求更新。服务器看到没有更新,所以它没有回答。相反,连接长时间保持打开。最终更新到来,服务器终于回答,或者客户端超时并终止连接。在这种情况下,客户端应立即重新发出查询以继续等待更新。



My Django app displays data from a database. This data changes without user intervention, i.e. 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. 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. All works fine. At the initial page load.

Now I find myself in a Python function that saves a new object to the database.

How do I tell the browser to run 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(...) ??? 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 ?

解决方案

two approaches:

  1. just update the database and wait until the next AJAX query. 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

    • use comet. In short: the client does an AJAX query asking for the update. 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.

这篇关于如何更新没有页面重新加载的Django页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆