问题描述:

Here is the code where the error occurs:

date_arrive = datetime.datetime.strptime(request.session.get('arrival'), '%m/%d/%Y'),\

date_depart = date_arrive + datetime.timedelta(days=request.session.get('nights')))).save()

And everytime Django reaches this view, it throws the error:

Exception Value: coercing to Unicode: need string or buffer, datetime.timedelta found

Here arrival is a valid datetime object and nights is an integer.

网友答案:

Id suggest you include full listing of function call.

Seems that you try concatenating unicode and datatime object, that is not possible. You have to convert date_arrive to datetime or "datetime.timedelta(days=request.session.get('nights')" to unicode evidently, depending on type your function needs as date_depart argument.

网友答案:

You should be able to solve this problem by making sure non-unicode/non-strings like integers and datetime objects are surrounded with a str(your_date_object_here).

When I encountered this error in Django I was able to fix it with both the following:

def __str__(self): 
    return str(self.datetimeobject) + " other string return info"

and

def __str__(self):
    return unicode(self.datetimeobject) + " other string return info" 
相关阅读:
Top