问题
I have a form in Django-python for an event program. I'm trying to create an ics file for the events with icalendar, for this, I want to get the values 'dtstart' and 'dtend' from the variables 'starttime' and 'endtime' in the form, but I'm getting the code: Wrong datetime format. Anyone with any advice to solve this issue?
ERROR
elif not ical[15:]:
return datetime(*timetuple)
elif ical[15:16] == 'Z':
return pytz.utc.localize(datetime(*timetuple))
else:
raise ValueError(ical)
except:
raise ValueError('Wrong datetime format: %s' % ical) …
class vDuration(object):
"""Subclass of timedelta that renders itself in the iCalendar DURATION
format.
"""
CODE
def event(request, id=None):
instance = Event_cal()
if id:
instance = get_object_or_404(Event_cal, pk=id)
else:
instance = Event_cal()
form = EventForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
startdate = request.POST.get('starttime')
endate = request.POST.get('endtime')
event = Event()
event.add('summary', 'My Summary')
event.add('dtstart', vDatetime.from_ical(startdate))
event.add('dtend', vDatetime.from_ical(endate))
Thanks in advance, I am learning python, so I don't have have much experience.
回答1:
Reformat the date times into one of the RFC5545 formats. Please see the RFC5545 specifications instructions for the datetime formats: https://tools.ietf.org/html/rfc5545#section-3.3.5.
There are 3 accepted datetime formats:
- Local or 'floating' eg: 19980118T230000
- Date with UTC time eg: 19980119T070000Z and
- Date with local time and timezone reference eg: TZID=America/New_York:19980119T020000
来源:https://stackoverflow.com/questions/64203439/django-icalendar-dtstart-datetime-issue