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