简体   繁体   中英

Exception Value: strptime() argument 1 must be str, not None

I am trying to get a user input(DateField) from my django app this code gives me the above error

Error Code:

def home(request):
today = datetime.date.today()
# If this is a POST request then process the Form data
if request.method == 'GET':

    form = DeadlineForm(request.GET)
    # instance = form.save()
    currentdate = datetime.date.today()
    print(currentdate)
    userinput = formd.data
    birthday = datetime.datetime.strptime(userinput,    '%m/%d/%Y').date()
    # print(birthday)
    days = birthday - currentdate
    daysLeft = 'Days to your birthday is ' ,+ days 
    return HttpResponse(daysLeft)
context = {
    'form': form,
    'today':today
}
return render(request, 'calculator/home.html', context)

But when I use string date format everything works fine but I want users to be able to insert their own date.

code with no error:

def home(request):
today = datetime.date.today()
# If this is a POST request then process the Form data
if request.method == 'GET':

    form = DeadlineForm(request.GET)
    # instance = form.save()
    currentdate = datetime.date.today()
    print(currentdate)
    # userinput = formd.data
    birthday = datetime.datetime.strptime('03/15/2019',    '%m/%d/%Y').date()
    # print(birthday)
    days = birthday - currentdate
    daysLeft = 'Days to your birthday is ' ,+ days 
    return HttpResponse(daysLeft)
context = {
    'form': form,
    'today':today
}
return render(request, 'calculator/home.html', context)

Please can someone show me how to get a string input from user.

Following is the syntax for strptime() method −

time.strptime(string[, format])

The following example shows the usage of strptime() method.

#!/usr/bin/python
import time

struct_time = time.strptime("30 Nov 00", "%d %b %y")
print ("returned tuple: %s " % struct_time)

When we run above program, it produces following result −

returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335, -1)

try this

def home(request):
   today = datetime.date.today()
   # If this is a POST request then process the Form data
   if request.method == 'POST':

       form = DeadlineForm(request.POST)
       # instance = form.save()
       currentdate = datetime.date.today()

       userinput = form.cleaned_data['date']
       or
       userinput = request.POST.get('date')

       birthday = datetime.datetime.strptime(userinput,    '%m/%d/%Y').date()
       # print(birthday)
       .....

in your userinput your are passing None value to strptime function

and use post method to get user input from Form

So first of all you are handling form input under wrong method

GET is responsible for rendering form, at that moment user didn't provided any data and that's why you get None

Pseudo code:

if request.method == 'GET':
  render_form
if request.method == 'POST':
  handle_form

You have a typo, to start with userinput = formd.data

The userinput you are targeting is a dictionary of the form inputs, either cleaned or not, you need to specify which field or input you want.

What you want to do is, - validate your form - get the field

def home(request):
   if request.method == 'GET':

       form = DeadlineForm(request.GET)
       if form.is_valid():
           userinput = form.cleaned_data['date']

           birthday = datetime.datetime.strptime(userinput, '%m/%d/%Y').date()

Thank You very much guys for the answers it was really helpful. I realize that the error "Exception Value: strptime() argument 1 must be str, not None" was because of the first strptime() argument was not a string so I actually used the str() function to convert the input to string. So everything works fine Now. So thank You once again.

Here is the code:

def home(request):
today = datetime.date.today()
form = CalculatorForm(request.POST)
# If this is a POST request then process the Form data

if form.is_valid():
    instance = form.save()
    currentdate = datetime.date.today()
    userinput = str(instance.deadline)
    birthday = datetime.datetime.strptime(userinput, '%Y-%m-%d').date()
    if birthday < datetime.date.today():
        return HttpResponse('Invalid date - You entered a date in the past')# print(birthday)
    days = birthday - currentdate
    print(days)
    # daysLeft = 'Days to your event is ' , days 
    # print(daysLeft)
    return render(request, 'calculator/check.html', {'days':days})
    ............
    ............

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM