简体   繁体   English

如何使用django实现实时?

[英]How can I use django to realize real time?

I have a rethinkdb. 我有一个rethinkdb。 Data will get in database for every five minutes. 每五分钟数据将进入数据库。 I want to create a website to real-time inspect this data flow from rethinkdb. 我想创建一个网站来实时检查来自rethinkdb的数据流。 That is, when surfing the webpage, the data from db on webpages can update automatically without refreshing the webpage. 也就是说,在浏览网页时,网页上db中的数据可以自动更新,而无需刷新网页。 I know there are several ways to make it real-time such as django channels or websockets. 我知道可以通过多种方式使其实时运行,例如django频道或websocket。 However, model in django does not support rethinkdb. 但是,Django中的模型不支持rethinkdb。

Sorry I am a layman of making website and may express things inaccurately. 抱歉,我是制作网站的外行,可能表达不正确的内容。 Can someone give me a keyword or hint? 有人可以给我一个关键字或提示吗?

If you make your question more specific, the community here will be able to offer you better support. 如果您的问题更具体,这里的社区将能够为您提供更好的支持。

However, here is a general solution to your problem. 但是,这是您问题的一般解决方案。

You will need to do two things: 您将需要做两件事:

  1. Create a backend API that allows you to: 创建一个后端API ,使您可以:

    • Check if new data has been added to the database 检查是否已将新数据添加到数据库中
    • Fetch new data via a REST api request 通过REST API请求获取新数据
  2. Make frontend AJAX requests to this api 对此API进行前端AJAX请求

    • Fetch data 获取数据
    • Periodically (every 30sec) check if there is new data 定期(每30秒)检查是否有新数据
    • Fetch data again if new data is detected 如果检测到新数据,则再次获取数据

To do this using Django as the backend, I would recommend using the Django Rest Framework to create your API. 要使用Django作为后端来执行此操作,我建议使用Django Rest Framework创建您的API。

This API should have two endpoints: 这个API应该有两个端点:

  1. ListView of your data 您的数据的ListView
  2. Endpoint returning the id and timestamp of the last datapoint 端点返回最后一个数据点的ID和时间戳

Next you will have to create a frontend that uses javascript to make requests to these endpoints. 接下来,您将必须创建一个使用javascript向这些端点发出请求的前端。 When you fetch data, store the id and timestamp of the most recent data point. 提取数据时,请存储最新数据点的ID和时间戳。 Use this to check if there is new data. 使用它来检查是否有新数据。

I would recommend using a Javascript framework such as Angular or react but depending on your needs these may be overkill. 我建议使用诸如Angular之类的Javascript框架,或做出反应,但根据您的需要,这些可能会过分地杀了。

EDIT: 编辑:

Now that you have updated your answer to be more specific, here is my advice. 既然您已经更新了答案以更具体,这是我的建议。 It sounds like your number one priority is rethinkDB and real time data. 听起来您的第一要务是rethinkDB和实时数据。 Django is not well suited this because it is not compatible with rethinkDB. Django不太适合此功能,因为它与rethinkDB不兼容。 Real time support has come a long way in Django with Django channels however. 但是,在具有Django通道的Django中,实时支持已经走了很长一段路。

It sounds like you are early on in your project and have little to no codebase in Django. 听起来您刚开始从事项目,在Django中几乎没有代码库。 I would recommend using horizon along with rethink db. 我建议与重新考虑数据库一起使用地平线 Horizon is a javascript backend built for real time data from rethinkdb. Horizo​​n是一个JavaScript后端,用于为rethinkdb提供实时数据。

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

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