强制在客户端的网页Django上重新加载 [英] Force reload on client's webpage Django

查看:82
本文介绍了强制在客户端的网页Django上重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django开发的新手,想知道是否有可能在客户端触发网页刷新。

I am new to Django development and would like to know is it possible to trigger a webpage refresh on client side.

我的场景:
网页1

My scenario: Webpage 1 is being surfed by clients.

我已将一些数据更新到数据库,并希望刷新view.py以从数据库中获取新数据并刷新客户端网页以显示新信息。 。

I updated some data to the database and wish to refresh the view.py to get new data from database and refresh clients webpage to display new informations.

如何实现?

谢谢。

最好的问候。

推荐答案

有两种选择:


  • 在页面上添加一个脚本,该脚本每隔几秒钟轮询一次服务器(ajax)以获取最新数据,并在适当位置刷新数据(不涉及页面刷新)。为此,您将在Django中创建一个视图,该视图以JSON格式返回数据,而您的JavaScript会替换HTML中的数据。 Pro:很棒的用户体验,页面仍然可用。缺点:不是立即开始的,您必须考虑可伸缩性,因为每个打开的页面都会不断轮询服务器,但是缓存响应应该很容易。

  • Add a script on your page that polls (ajax) your server every few seconds for the latest data and refresh the data in place (no page refresh involved). For this you'd create a view in Django that returns the data in JSON format and your javascript would replace the data in the HTML. Pro: great user experience, page remains available. Con: Not immediate, you'd have to think of scalability as each open page will be constantly polling your server, but caching the response should be easy.

添加一个脚本,该脚本仅轮询是否应刷新页面。它将发送某种版本的令牌,以标识页面上的当前数据,并且您的Django视图可以告诉您是否需要刷新页面。然后脚本将只调用 location.reload()刷新页面。优点:不需要获取数据,刷新状态可以轻松地缓存;缺点:仍然不是立即的,当用户可能不期望它时会发生页面刷新。

Add a script that just polls whether the page should be refreshed. It would send some kind of version number of token identifying the current data on the page and your Django view could tell whether or not the page needs to be refreshed. Then the script would just call location.reload() to refresh the page. Pro: no need to fetch the data, refresh state can easily be cached, Con: still not immediate, page refresh happens when user might not be expecting it.

使用网络套接字将开放的永久连接打开到服务器,您可以通过该永久连接推送新数据(或仅发送事件:请刷新)。专业版:最佳体验,立即更新,缺点:在旧版浏览器上不起作用,实施和部署都很复杂。 django-channels 是一个很好的入门者。

Use a websocket to have an open permanent connection open to your server through which you can push the new data (or just the event: 'please refresh'). Pro: best experience, immediate update, Con: doesn't work on old browsers, complex to implement and deploy. django-channels would be a good starter.

这篇关于强制在客户端的网页Django上重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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