title: Django - Update the final result in views.py every n seconds
body: <p>I built a stock screener that I want to update the final result based on the latest prices during the market hours.
  I simplify my original files for better understanding of what I really need.</p>
  <p>models.py</p>
  <pre><code>class ScreenerModel(models.Model)
      pct_choices = [(0, 'all'), (1, 'above 1'), (2, 'below 1')]
      price_choices = [(0, 'all'), (1, 'above 5000'), (2, 'below 5000')]
      
      user = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
      pct = models.IntegerField(choices=pct_choices, default=0)
      price = models.IntegerField(choices=price_choices, default=0)
  </code></pre>
  <p>urls.py</p>
  <pre><code>urlpatterns = [
  path('users/tmp/&lt;int:pk&gt;/', views.ScreenerModelUser, name='tmp_user'),
  ]
  </code></pre>
  <p>views.py</p>
  <pre><code>import pandas as pd
  from .models import ScreenerModel
  from django.contrib.auth.decorators import login_required
  from django.shortcuts import render
  
  @login_required
  def ScreenerModelUser(request, pk):
      user_screener = ScreenerModel.objects.get(user_id=request.user.id, pk=pk)
      df = pd.read_csv('https://raw.githubusercontent.com/AmirForooghi/stocks_csv/master/stocks_sample.csv')
  
      pct_user = user_screener.pct
      if pct_user == 1:
          df = df.loc[df.pct &gt; 1]
      if pct_user == 2:
          df = df.loc[df.pct &lt; 1]
  
      price_user = user_screener.price
      if price_user == 1:
          df = df.loc[df.close &gt; 5000]
      if price_user == 2:
          df = df.loc[df.close &lt; 5000]
  
      context = {'df': df.to_html()}
      return render(request, 'screener/tmp_user.html', context)
  </code></pre>
  <p>tmp_user.html</p>
  <pre><code>{{ df|safe }}
  </code></pre>
  <p><strong>My Problem</strong>: As you can see I have online prices in the <code>df</code> and in the views I just filter them according to the user preference. Obviously the prices change during market hours. I want to update the final result in the template according to the latest prices every n seconds without the user refreshing the page and as long as the URL is open in the browser.</p>
  <p>What I have tried so far are listed below:</p>
  <p>Add a result field to the models.py to store the final result. and save the result every n seconds in the views.py.</p>
  <p>models.py</p>
  <pre><code>class ScreenerModel(models.Model)
      pct_choices = [(0, 'all'), (1, 'above 1'), (2, 'below 1')]
      price_choices = [(0, 'all'), (1, 'above 5000'), (2, 'below 5000')]
      
      user = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
      pct = models.IntegerField(choices=pct_choices, default=0)
      price = models.IntegerField(choices=price_choices, default=0)
      result = models.CharField(max_length=10000, blank=True, default='')
  </code></pre>
  <p>views.py</p>
  <pre><code>import pandas as pd
  import time
  from .models import ScreenerModel
  from django.contrib.auth.decorators import login_required
  from django.shortcuts import render
  
  @login_required
  def ScreenerModelUser(request, pk):
      while True:
          user_screener = ScreenerModel.objects.get(user_id=request.user.id, pk=pk)
          df = pd.read_csv('https://raw.githubusercontent.com/AmirForooghi/stocks_csv/master/stocks_sample.csv')
  
          pct_user = user_screener.pct
          if pct_user == 1:
              df = df.loc[df.pct &gt; 1]
          if pct_user == 2:
              df = df.loc[df.pct &lt; 1]
  
          price_user = user_screener.price
          if price_user == 1:
              df = df.loc[df.close &gt; 5000]
          if price_user == 2:
              df = df.loc[df.close &lt; 5000]
  
          user_screener.result = df.to_json()
          user_screener.save()
  
          context = {'df': df.to_html()}
          return render(request, 'screener/tmp_user.html', context)
          time.sleep(30)
  </code></pre>
  <p>And it didn't address the issue. I know in this case javascript and AJAX are needed but I am still learning them right now.</p>
  <p>I also check this <a href="https://stackoverflow.com/questions/29721897/django-python-updating-the-database-every-second">link</a> but couldn't solve my problem.</p>
  <p>I really appreciate if you can specifically solve this issue or if my entire approach is wrong in doing this, just give a correct approach or even a starting point.</p>