CreateView works but new data not displayed in ListView

Hi,
I’m using CreateView for an input form. This works in the sense that the record is created (I can see it in /admin) but the list view does not show the added records unless I restart the server.

models.py

class Strike(models.Model):
  STRIKE_ACTIVITY = (
    ('TW', 'TW'),
    ('TB', 'TB'),
    ('Tickets', 'Tickets'),
    ('Other', 'Other'),
  )
  player = models.ForeignKey(Player, on_delete=models.CASCADE)
  strike_date = models.DateField()
  ishard = models.BooleanField(default=True)
  comments = models.TextField(max_length=200, blank=True)
  activity = models.CharField(max_length=10, choices=STRIKE_ACTIVITY)
  
  class Meta:
    ordering = ["-strike_date"]
  
  def __str__(self):
    return f'{self.player}, {self.strike_date}, {self.ishard}, {self.activity}, {self.comments}'

views.py

from datetime import date, timedelta
from django.views.generic import CreateView, ListView

class StrikeCreateView(CreateView):
  model = Strike
  fields = ['player', 'strike_date', 'activity', 'ishard', 'comments']
  success_url = '/strikes/'

class StrikeListView(ListView):
  model = Strike
  context_object_name = 'strikes'
  ActiveDate = date.today() - timedelta(days=30)
  active_strikes = Strike.objects.filter(strike_date__gte=ActiveDate).order_by('-strike_date')
  legacy_strikes = Strike.objects.filter(strike_date__lt=ActiveDate).order_by('-strike_date')
  print(active_strikes)
  extra_context={'active_strikes':active_strikes, 'legacy_strikes':legacy_strikes}

urls.py

from django.urls import path
from . import views

urlpatterns = [
  path('strikes/', views.StrikeListView.as_view(), name='strike.list'),
  path('strikes/new', views.StrikeCreateView.as_view(), name='strike.new'),
]

strike_form.html

{% extends './base.html' %}
{% load static %}
{% load crispy_forms_tags %}

{% block content %}

<div class="row mx-5">
  <div class="col text-start">
    <form action="{% url 'strike.new' %}" method="post">
      {% csrf_token %}
      {{ form|crispy }}
      <button type="submit" class="btn btn-success">Strike!</button>
    </form>
  </div>
</div>
  
{% endblock content %}

strike_list.html

{% extends './base.html' %}
{% load humanize %}

{% block content %}
<div class="row">
  <div class="col">
    <div class="player_list_heading mb-3">
      <h1 class="display-1">Strikes</h1>
    </div>
  </div>
</div>
<div class="row">
  <h5 class="display-6">Active</h5>
  {% for strike in active_strikes %}
  <div class="col-md-2 ">
    <div class="card text-black">
      <div class="card-header">
        {{ strike.player.playerName }}
      </div>
      <div class="card-body">
        <p>{{ strike.strike_date }}</p>
        <p>Activity: {{ strike.activity }}</p>
        <p>Hard: {{ strike.ishard }}</p>
        <p>Comments: {{ strike.comments }}</p>
      </div>
    </div>
  </div>
  {% endfor %}
</div>
<div class="row mt-4">
  <h5 class="display-6">Legacy</h5>
  {% for strike in legacy_strikes %}
  <div class="col-md-2 ">
    <div class="card text-black">
      <div class="card-header">
        {{ strike.player.playerName }}
      </div>
      <div class="card-body">
        <p>{{ strike.strike_date }}</p>
        <p>Activity: {{ strike.activity }}</p>
        <p>Hard: {{ strike.ishard }}</p>
        <p>Comments: {{ strike.comments }}</p>
      </div>
    </div>
  </div>
  {% endfor %}
</div>
{% endblock content %}

Any thoughts?

Thanks in advance,
Brian

This is not how you customize a Class-Based Generic view.

When a view is imported, class variables are set at the time the module is imported, and are going to remain constant for the duration of that instance.

So, if you want to change the values on a per-instance basis, you need to override the appropriate functions within the CBV to return the right values at the right point in time.

If you’re not familiar with Python’s classes and how they work, you may wish to find a good tutorial that really explains them.

Once you’re comfortable with them, then you’ll want to review the full set of CBV docs starting with Class-based views | Django documentation | Django. I also recommend the Classy Class-Based Views site and the CBV diagrams page.

Thank you for your response. I have been through the docs a few times. I’ve got this all working in v3.2 and am learning 4.1 now, so am learning about the class based generic views. I am familiar with Python classes. What I’m trying to do is seemingly simple (split a dataset in two parts based on date) but maybe I should just use the older model forms method instead? Or is there a better way to achieve this, perhaps with if/then in the template?

Ok, I did this and it works fine:

class StrikeListView(ListView):
  model = Strike
  # context_object_name = 'strikes'
  def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        ActiveDate = date.today() - timedelta(days=30)
        context['strikes_active'] = Strike.objects.filter(strike_date__gte=ActiveDate).order_by('-strike_date')
        context['strikes_legacy'] = Strike.objects.filter(strike_date__lt=ActiveDate).order_by('-strike_date')
        return context