I'm trying to save the user's choice of the checkbox for whether they have completed an event or not. I just want the checkbox to be able to save the user's previous choice on a page refresh. Every time the page refreshes the value is set back to False, or, unchecked.
utils.py
def formatday(self, day, events):
events_per_day = events.filter(start_time__day=day)
d = ''
for event in events_per_day:
d += f'<li><label> {event.get_html_url} <input type="checkbox"></label></li>'
if day != 0:
return f"<td><span class='date'>{day}</span><ul> {d} </ul></td>"
return '<td></td>'
views.py
def event(request, event_id=None):
instance = Event()
if event_id:
instance = get_object_or_404(Event, pk=event_id)
else:
instance = Event()
form = EventForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
event = Event.objects.create(**form.cleaned_data, user=request.user)
print(event.title)
return HttpResponseRedirect(reverse('cal:calendar'))
return render(request, 'cal/event.html', {'form': form})
models.py
class Event(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
description = models.TextField(max_length=350)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
event_checked = models.BooleanField(default=False, blank=True)
@property
def get_html_url(self):
url = reverse('cal:event_edit', args=(self.id,))
return f'<a href="{url}">{self.title}</a>'
forms.py
class EventForm(forms.ModelForm):
#event_done = forms.BooleanField()
class Meta:
model = Event
# datetime-local is a HTML5 input type, format to make date time show on fields
widgets = {
'start_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'),
'end_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'),
}
fields = ('title', 'description', 'start_time', 'end_time')
def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
# input_formats parses HTML5 datetime-local input to datetime field
self.fields['start_time'].input_formats = ('%Y-%m-%dT%H:%M',)
self.fields['end_time'].input_formats = ('%Y-%m-%dT%H:%M',)
Aucun commentaire:
Enregistrer un commentaire