1👍
Without knowing the full context, I can see two possible solutions.
1) In your Django view, load the model and calculate the the time since its beginning datetime, run a function to calculate your current auto-incrementing number, and add it to the view’s context for display. Then use JavaScript to auto-update the number after the view loads in the browser. (Not a very exact method).
2) You could create a custom Django manage.py command and then use crontab to auto-execute the command at a set interval. This would allow you to store the changing number in the database. You could then use AJAX (something like jQuery’s $.get() may be easy to implement) to pull that value from a Django view that serves the current number from the database. Make sure to have the AJAX script run on a set interval.
2👍
Maybe this can help: Advanced Python Scheduler
Here’s a small piece of code from their documentation to run every hour or configure as crontab:
from apscheduler.schedulers.blocking import BlockingScheduler
def some_job():
print "Decorated job"
scheduler = BlockingScheduler()
scheduler.add_job(some_job, 'interval', hours=1)
scheduler.start()
Without knowing more of your code it’s difficult to say on incremenation but I can suggest
number += 1
2👍
You might have a model like this to store the integer counter in the database:
class Counter(models.Model):
counter_value = models.IntegerField()
Then you can create an instance of this model and give it an initial value of 0
using Django shell: Enter the shell by running python manage.py shell
, then run the following after importing the Counter
model:
Counter.objects.create(counter_value=0)
Then create a view that increments the counter (after importing your Counter
model):
def increment(request):
counter = Counter.objects.get(pk=1)
counter.counter_value += 1
counter.save()
Then create a url
for this view (after importing the corresponding views
):
urlpatterns = [
# ...
url(r'^increment-counter/', views.increment)
]
Now, you can run a simple Python script on the server to request the url above and cause the counter to increment every one hour for example. The script may look like this:
import time
import requests
while True:
request.get('http://your-website-url/increment-counter')
time.sleep(60*60) # 1 hour
This requires that you have the requests package installed.
After that, you can run this script on your server (python3 /path/to/script
) to achieve what you wanted.
I kept it simple. You can, however, add complexity to satisfy your needs. You can also make the Python script run when the system starts, and you can add more functionality to it.
Note: I haven’t tested the code above, but I think it is simple and works as expected.
- [Django]-What is the difference between permission_classes and authentication_classes in django rest framework generic API Views
- [Django]-How to use TimeInput widget in Django forms?
- [Django]-Django models – field dependency
- [Django]-Unable to create a custom admin template url, getting errors Template errors & creating custom admin site errors
- [Django]-Cumulative (running) sum of field Django and MySQL