简体   繁体   中英

How can I repeatedly call a function from a views.py in Django?

How can I repeatedly call a function from a views.py in Django?

urls.py

urlpatterns = [
    url(r'^$', views.index),
    url(r'^report/(?P<extension>\d+)/$', views.report),
]

views.py

def report(request, extension):

    """
        I will do some logic here. I need the extension variable for 
        database purposes.
      EX: 
        my_array = Report.objects.fetching_reports(extension='3')
    """

    return render(request, 'report.html’)

If you notice in extension, I passed in 3. The idea is that each extension will have their own data. However, I want them to render on the same html template. I will begin rendering from extension 1, up to 12 then goes back to 1. Let's say extension 4 is missing, it will be redirected to extension 5.These extension will come from my database.

Example:

…/report/1/
…/report/2/
…/report/3/    ## will skip 4 if not available 
…/report/5/
…/report/6/    ## and so on..

Each extension will render the same HMTL template. Right now, I can successfully render these reports if I type the URL patter directly to the browser. Is there a way to call report( ) continuously, let's say every 15 seconds? Or should I have a different approach to this?

Thank you very much for reading.

def report(request, extension):

    try:
        my_array = Report.objects.fetching_reports(extension=extension)
    except Report.DoesNotExist:
        my_array = None

    extension +=1

    if my_array = None:
        return HttpResponseRedirect(reverse('your_project_name':'your_app_name', kwargs = {'extension':extension}))
    else:
        return render(request, 'report.html', {'extension':extension})

that will skip to the next object if 4 doesn't exist, if you wanna skip after 15 seconds you can do a JavaScript redirecting to the next page and using extension var

For each report, pass a next url to the template and use javascript to reload.

<script>
  function nextReport() { window.location.href = "{{ next }}"; }
  setTimeout(nextReport, 15000);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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