Re: 'NoneType' object has no attribute 'datetime' bug?

1,325 views
Skip to first unread message

Ernesto Guevara

unread,
Jul 14, 2012, 1:15:56 PM7/14/12
to django...@googlegroups.com
Try this:

from datetime import datetime, time, date

current_date = datetime.now()

today = datetime.combine(date.today(), time(19,30))

Look:

>>> from datetime import datetime, time, date
>>> print datetime.now()
2012-07-14 14:14:17.897023
>>> print datetime.combine(date.today(),time(19,30))
2012-07-14 19:30:00
>>>

o/


2012/7/14 dobrysmak <lukasz.s...@gmail.com>
Hi guys,
i have run into this.

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
20. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/home/user/dev/xyz/coupon/views.py" in enter_game
69. Coupon(coupon=coupon, player=player, credicts=credicts).save()
File "/home/user/dev/xyz/coupon/models.py" in save
253. self.coupon.close_coupon()
File "/home/user/dev/xyz/coupon/models.py" in close_coupon
78. current_date = datetime.datetime.now()

Exception Type: AttributeError at /wejdz-do-gry/49/
Exception Value: 'NoneType' object has no attribute 'datetime'
Request information:
GET: No GET data

and the cuntion "close_coupon" looks like this

class Coupon(models.Model): .
.
. end_date = models.DateTimeField(default=None, null=True, blank=True)
def close_coupon(self):
current_date = datetime.datetime.now() today = datetime.datetime.combine(datetime.date.today(), datetime.time(19,30))
.
  .
end_date = current_date

Don't know if that's a bug. Does anyone know how to deal with this issue?
Cheers!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/iH-0-noK-zkJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

dobrysmak

unread,
Jul 16, 2012, 4:21:00 AM7/16/12
to django...@googlegroups.com
Thank's for help , i did this and now i've got this 

AttributeError at /wejdz-do-gry/76/
'NoneType' object has no attribute 'now'

This is strange because the shell import works fine but the server script throus an exception.

>>> from datetime import datetime, time, date
>>> print datetime.now()
2012-07-14 14:14:17.897023
>>> print datetime.combine(date.today(),time(19,30))
2012-07-14 19:30:00
>>> 
Ok, that's true but the script does not even reach the 'combine' function it died on 'now'. 

 
2012/7/14 dobrysmak <lukasz.s...@gmail.com>
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

tWoolie

unread,
Jul 16, 2012, 5:15:52 AM7/16/12
to django...@googlegroups.com
somehow, your global datetime variable is being clobbered with the value None. Find where that happens, and your bug will disappear.

Melvyn Sopacua

unread,
Jul 30, 2012, 8:16:39 AM7/30/12
to django...@googlegroups.com
On 14-7-2012 14:01, dobrysmak wrote:

> def close_coupon(self):
> current_date = datetime.datetime.now() today =
> datetime.datetime.combine(datetime.date.today(), datetime.time(19,30))
> .
> .
> end_date = current_date

This function is stripped down I suppose, because it does nothing useful:
- it changes a few local variables (not values of the model)
- today is unused
- end_date is set to current_date which seems odd.

My guess is that you want the end_date set to half past seven of the
current_date:

def close_coupon(self) :
from django.utils import timezone
current_date = timezone.now()
end_date = current_date.replace(hour=19, minutes=30)
self.current_date = current_date
self.end_date = end_date

--
Melvyn Sopacua

Masklinn

unread,
Jul 30, 2012, 8:27:54 AM7/30/12
to django...@googlegroups.com

On 2012-07-16, at 10:21 , dobrysmak wrote:

> Thank's for help , i did this and now i've got this
>
> AttributeError at /wejdz-do-gry/76/
> 'NoneType' object has no attribute 'now'
>
> This is strange because the shell import works fine but the server script
> throus an exception.

Sounds like you have a parameter called "datetime" or you create a
variable called "datetime" somewhere, which clobbers the module.

Check the rest of the file, you surely have a line along the lines of
`datetime = None` somewhere.

Peter of the Norse

unread,
Aug 15, 2012, 12:42:10 AM8/15/12
to django...@googlegroups.com
I had a similar problem in one of my views. It looked like:

from X import y

def my_func():
foo = y.baz()
...
y = foo + bar;

Since I was assigning to y later in the function, “y” represented a local variable throughout the function. Somewhere you have “datetime =”, but it might be later in the method.
> def close_coupon(self):
>
> current_date = datetime.datetime.now()
> today = datetime.datetime.combine(datetime.date.today(), datetime.time(19,30))
>
> .
> .
> end_date = current_date
>
> Don't know if that's a bug. Does anyone know how to deal with this issue?
> Cheers!

Peter of the Norse
Rahm...@Radio1190.org



Reply all
Reply to author
Forward
0 new messages