In my application user is entering his name, email, meeting date and meeting hour and I want to save all these information to my database. But application is not running. I'm getting TypeError: strptime() argument 1 must be str, not None
but don't what is the reason. Everything seems like true.
models.py
:
def index(request):
context = {
'schedules': Schedule.objects.all()
}
participant_name = request.POST.get('name')
participant_email = request.POST.get('email')
meeting_date = request.POST.get('date')
meeting_hour = request.POST.get('hour')
converted_meeting_date = datetime.strptime(request.POST.get('date'), "%m-%d-%Y")
converted_meeting_hour = datetime.strptime(request.POST.get('hour'), "%H:%M")
if request.POST.get('participant_email'):
Schedule.objects.create(
participant_name = request.POST.get('name'),
participant_email = request.POST.get('email'),
meeting_date = converted_meeting_date,
meeting_hour = converted_meeting_hour
)
return render(request, 'index.html', context)
models.py
:
class Schedule(models.Model):
participant_name = models.CharField(max_length=100)
participant_email = models.CharField(max_length=100)
meeting_date = models.DateField()
meeting_hour = models.TimeField()
is_scheduled = models.BooleanField(default=False)
def __str__(self):
return self.participant_name
You need to check if both meeting_date and meeting_hour are not None and also you need to convert them into string before applying strptime to them.
def index(request):
context = {
'schedules': Schedule.objects.all()
}
participant_name = request.POST.get('name')
participant_email = request.POST.get('email')
meeting_date = str(request.POST.get('date'))
meeting_hour = str(request.POST.get('hour'))
converted_meeting_date = datetime.strptime(request.POST.get('date'), "%m-%d-%Y").date() if meeting_date else None
converted_meeting_hour = datetime.strptime(request.POST.get('hour'), "%H:%M").time() if meeting_hour else None
if request.POST.get('participant_email'):
Schedule.objects.create(
participant_name = request.POST.get('name'),
participant_email = request.POST.get('email'),
meeting_date = converted_meeting_date,
meeting_hour = converted_meeting_hour
)
return render(request, 'index.html', context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.