我正在尝试在日历中的事件旁边添加一个复选框。我还希望它保存为用户保留的任何内容(选中/未选中)。
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})
class CalendarView(generic.ListView):
model = Event
template_name = 'cal/calendar.html'
def get_queryset(self):
qs = Event.objects.filter(user=self.request.user.id)
return qs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
d = get_date(self.request.GET.get('month', None))
cal = Calendar(d.year, d.month)
cal.user = self.request.user
html_cal = cal.formatmonth(withyear=True)
context['calendar'] = mark_safe(html_cal)
context['prev_month'] = prev_month(d)
context['next_month'] = next_month(d)
return context
模型.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()
@property
def get_html_url(self):
url = reverse('cal:event_edit', args=(self.id,))
return f'<a href="{url}"> {self.title} </a>'
def __str__(self):
return '{} - {} by {}'.format(self.title, self.description, self.user)
Heres what my calendar looks like with an event added
我是一名优秀的程序员,十分优秀!