The task is to update the values ​​of specific HTML tags by timer, without reloading the entire page, the code below requests a Django representation in a JS loop.

urls.py

from django.conf.urls import include, url from opc_app import views from opc_app.views import * urlpatterns = [ url(r'Main_DateTime/', DateTime_main_func), url(r'Template_DateTime/', Actual_DateTime_template_func, name="Template"), ] 

views.py

 from __future__ import unicode_literals from django.shortcuts import render import datetime from datetime import time from django.http import HttpResponse def Actual_DateTime_template_func (request): now_date = datetime.datetime.now() out_values = { "DateTime": now_date, "Time": now_date.time(), } return render (request, "Actual_DateTime_template.html", context = out_values) def DateTime_main_func (request): return render (request, "Main_DateTime.html") 

Actual_DateTime_template.html

 {{ DateTime }} <br> {{ Time }} 

Main_DateTime.html

 <body> <p class="Rand_Time"></p> <p class="Rand_Time"></p> <p class="Rand_Time"></p> <p class="Rand_Time"></p> <script> $(document).ready(function(){ Call_Update_func(); function Call_Update_func() { $('.Rand_Time').load("{% url 'Template' %}"); setTimeout(Call_Update_func, 1000); } }); </script> </body> 

The problem is that you need to display different Django variables in each tag, if you select id, you get to write a function for each variable, advise how you can organize an optimal data sample, thank you!

  • "you need to display different variables in each tag" - details? - Igor
  • I tried to use the GetElementById method, but you need to create a separate template for each tag, can you advise a more correct solution? - Georgy
  • $ (document.getElementById ("Date")). load ("{% url 'Template'%}"); $ (document.getElementById ("Time")). load ("{% url 'Template'%}"); - Georgy

1 answer 1

Solved the problem by loading the table row with variables already

Actual_DateTime_template.html

 <tr> <td >{{ DateTime }}</td> <td >{{ Time }}</td> <td ></td> <td ></td> </tr>