在 Django 中定期运行一个函数

发布于 2024-09-17 20:16:33 字数 279 浏览 5 评论 0原文

我在 Django 中编写一个应用程序来监视一组服务器中的某些服务。我希望定期更新观点。到目前为止,我已经研究了编写自定义管理命令(链接在这里< /a>) 并在我的模板中有 {% ifchanged %} 标签。我只是想知道这是否是最好的方法,或者是否有更好的方法,例如自动刷新网页的某些部分或其他内容。谢谢。

Im writing an app in Django to monitor certain services in a group of servers. I would like to have the views updated periodically. So far I've looked at writing custom admin commands (link here) and have {% ifchanged %} tags in my template. I just wanted to know if this is the best approach or if there is a better way to do it, like auto refresh certain portions of the webpage or something else. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫多说 2024-09-24 20:16:33

如果您想定期运行某些内容,我建议您查看 celery 任务(请参阅 此处了解简短教程)。

从那里,就像 Craig Trader 提到的那样,您可以使用 AJAX 来检查任务的状态 (django-celery 有一些内置的 视图url 您可以使用)。

If you're wanting to run something periodically, I would suggest looking at celery tasks (see here for a brief tutorial).

From there, like Craig Trader mentioned, you could use AJAX to check the status of the tasks (django-celery has some built in views and urls you can use).

小情绪 2024-09-24 20:16:33

ifchanged 模板标记 实际上适用于当您在模板中处理一系列对象,并且希望在字段从一个对象更改为下一个对象时执行某些操作的情况。那可能无法满足您的需求。

如果您只想定期更新支持模型的数据,那么编写自定义命令并使用 cron 作业定期运行它是最有意义的。

如果您希望用户界面定期刷新,有很多方法可以做到这一点:元刷新标签、javascript/jquery 技巧,甚至使用 AJAX 来更新显示的数据。

The ifchanged template tag is really for when you're processing a series of objects within a template, and want to do something if a field changes from one object to the next. That probably won't meet your needs.

If you just want the data backing your models to be updated periodically, then writing a custom command and running it periodically with a cron job makes the most sense.

If you want the user interface to refresh itself periodically, there are lots of ways to do that: meta-refresh tags, javascript/jquery tricks, or even using AJAX to update the displayed data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文