I am writing a little app where the user creates an event and specifies the date that event will occur. After the event date has past, I want to delete that event instance. My current attempt is throwing a function that checks if the event should expire in the event page view. I am not sure whether the expiration_check function is checking in a correct way, nor am I sure whether just having a function in the view will event work.
Here is my view and expire function:
def event_page(request, name):
event = Event.objects.get(name=name)
check_expiration(event)
if request.method == "POST":
form = GuestForm(request.POST)
if form.is_valid():
Guest = form.save(commit=False)
Guest.event = event
Guest.save()
return redirect(event)
else:
form = GuestForm()
return render(request, "event_page.html", {"form": form, "event": event, })
def check_expiration(event):
now = datetime.datetime.now()
if event.date < now: #if the event date has past
event.delete()
I collect the date from the user and store it in a DateTime filed: date = models.DateField()
Let me know if any further details are needed. Any insight is appreciated, thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…