开发者

Running a function periodically in Django

开发者 https://www.devze.com 2023-01-15 00:11 出处:网络
Im writing an app in Django to monitor certain ser开发者_如何学Govices in a group of servers. I would like to have the views updated periodically. So far I\'ve looked at writing custom admin commands

Im writing an app in Django to monitor certain ser开发者_如何学Govices 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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消