N
N
Nur Kasimov2017-01-14 12:18:46
Django
Nur Kasimov, 2017-01-14 12:18:46

Why is the controller class in django inherited from ListView not updating the data after changing the database?

Here is the code for this controller in views.py

# python 3.5, django 1.9
from django.utils import timezone
from creation.models import Game
from django.views.generic.list import ListView

class GameList(ListView):
    template_name = "gamelist.html"
    queryset = Game.objects.order_by("-end")
    paginate_by = 20
    def get_context_data(self, **kwargs):
        context = super(GameList, self).get_context_data(**kwargs)
        context["User"] = self.request.user
        context["next"] = []
        context["now"] = []
        context["time"] = timezone.now()
        for i in self.queryset:
            if i.start > timezone.now():
                context["next"].append(i)
            elif i.end > timezone.now():
                context["now"].append(i)
            else: break
        context["next"].reverse()
        return context

Actually, it divides all games into the next, current ones, which are displayed on the page respectively from arrays, and the past games from the object_list by comparing game.end < time because it has all the games. There will always be few future and current games, so past games need more pagination.
{% if next %}
     <p class="next"></i> Ближайшие игры:</p>
     {% for game in next %}
         <a href="{% url "infogame" code=game.code %}"> {{ game.name }} (начало: {{ game.start }})</a><br />
     {% endfor %}
{% endif %}
{% if now %}
    <p class="now">Текущие игры:</p>
    {% for game in now %}
        <a href="{% url "infogame" code=game.code %}">> {{ game.name }} (до: {{ game.start }})</a><br />
    {% endfor %}
{% endif %}
<p class="games">Все прошедшие игры:</p>
{% for game in object_list %}
     {% if game.end < time %}  <!-- игра завершена -->
            <a href="{% url "infocart" code=game.code %}">> {{ game.name }} ({{ game.start }} - {{ game.end }})</a><br />
      {% endif %}
{% endfor %}

Everything is displayed correctly, but if you change the start and end time of the game so that it moves to a different category, it does not display at all. After restarting the server, everything is as it should be.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ArkadyBagdasarov, 2017-01-23
@ArkadyBagdasarov

The thing is that the template is generated once - during the page load. To make the content change dynamically - load data via ajax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question