Tutorial 1: AttributeError at /time/ 'module' object has no attribute 'now'

1,674 views
Skip to first unread message

Nick Mellor

unread,
Feb 9, 2010, 2:09:40 PM2/9/10
to Django users
Hi all,

This must be very simple, but I'm stumped.

Here's the code for views.py

from django.http import HttpResponse
import datetime

def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)

The offending line is:
now = datetime.datetime.now()

The code's cut and pasted from the Django 1.1 tutorial (http://
docs.djangoproject.com/en/1.1/topics/http/views/), and I have Django
1.1.1.

datetime.datetime.now() works as a function call at the Python shell:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2010, 2, 10, 5, 17, 21, 218000)

Any ideas? Thanks,

Nick

Shawn Milochik

unread,
Feb 9, 2010, 2:22:11 PM2/9/10
to django...@googlegroups.com
Perhaps replace the bare 'now' with a tuple:

Before:


> html = "<html><body>It is now %s.</body></html>" % now

After:
> html = "<html><body>It is now %s.</body></html>" % (now,)

Shawn

Nick Mellor

unread,
Feb 9, 2010, 5:19:17 PM2/9/10
to Django users
Thanks Shaun,

I renamed the "now" variable as "dt" but it didn't help. I've also
tried your 1-tuple idea-- no change.

Note that the offending line isn't the html assignment but:

now = datetime.datetime.now()

Mike Ramirez

unread,
Feb 9, 2010, 5:24:12 PM2/9/10
to django...@googlegroups.com
On Tuesday 09 February 2010 14:19:17 Nick Mellor wrote:
> Thanks Shaun,
>
> I renamed the "now" variable as "dt" but it didn't help. I've also
> tried your 1-tuple idea-- no change.
>
> Note that the offending line isn't the html assignment but:
>
> now = datetime.datetime.now()
>

Make sure your import line is correct.

In [1]: import datetime

In [2]: datetime.datetime.now()
Out[2]: datetime.datetime(2010, 2, 9, 14, 23, 25, 155503)

--
Decemba, n: The 12th month of the year.
erra, n: A mistake.
faa, n: To, from, or at considerable distance.
Linder, n: A female name.
memba, n: To recall to the mind; think of again.
New Hampsha, n: A state in the northeast United States.
New Yaak, n: Another state in the northeast United States.
Novemba, n: The 11th month of the year.
Octoba, n: The 10th month of the year.
ova, n: Location above or across a specified position. What the
season is when the Knicks quit playing.
-- Massachewsetts Unabridged Dictionary

signature.asc

Shawn Milochik

unread,
Feb 9, 2010, 5:27:47 PM2/9/10
to django...@googlegroups.com

On Feb 9, 2010, at 5:24 PM, Mike Ramirez wrote:

> On Tuesday 09 February 2010 14:19:17 Nick Mellor wrote:
>> Thanks Shaun,
>>
>> I renamed the "now" variable as "dt" but it didn't help. I've also
>> tried your 1-tuple idea-- no change.
>>
>> Note that the offending line isn't the html assignment but:
>>
>> now = datetime.datetime.now()
>>
>
> Make sure your import line is correct.
>
> In [1]: import datetime
>
> In [2]: datetime.datetime.now()
> Out[2]: datetime.datetime(2010, 2, 9, 14, 23, 25, 155503)
>
> -


To clarify this, if you are doing:

from datetime import datetime

then you'd need to use datetime.now() instead of datetime.datetime.now().

Shawn

Karen Tracey

unread,
Feb 9, 2010, 5:29:00 PM2/9/10
to django...@googlegroups.com
On Tue, Feb 9, 2010 at 5:19 PM, Nick Mellor <nick.mell...@pobox.com> wrote:
Thanks Shaun,

I renamed the "now" variable as "dt" but it didn't help. I've also
tried your 1-tuple idea-- no change.

Note that the offending line isn't the html assignment but:

now = datetime.datetime.now()


It seems that whatever datetime is there in you view, it is not the datetime that is expected. You can put a breakpoint right before that line in you view and then investigate what is is you really have there.  For example:

> /home/kmt/software/web/xword/crossword/views.py(39)main()
-> now = datetime.datetime.now()
(Pdb) datetime
<module 'datetime' from '/usr/lib/python2.5/lib-dynload/datetime.so'>
(Pdb)

Perhaps what you actually have for datetime will give a clue how datetime got to be something other than the standard datetime module.

Karen

Bill Freeman

unread,
Feb 9, 2010, 5:34:50 PM2/9/10
to django...@googlegroups.com
Your code snippet works for me (though I'm just returning html rather than
the HttpResponse object, for testing purposes).

Be very sure that the spelling of everything in the file in question is correct
and that you aren't, for example, using the name "datetime" for something
else between the import and the usage.

If you're running in the development server, it is easy to stick in
pdb.set_trace()
(you'll have to import pdb) just before the offending line, and poke around when
you hit the breakpoint to see what datetime really is at that point.

If you don't hit the breakpoint, then you're not running with the file
you're editing.

Be sure that your really said "import datetime" rather than
"from datetime import datetime" (which is the way that I typically
import it) or that
someone else hasn't done that after your import.

If you're not running in the development server, then you might replace your
"now = ..." and "html = ..." lines with:
html = "<html><body>datetime is %r.</body></html>" % datetime
which will tell you what datetime is at that point. You will have to
restart apache
(or perhaps touch your .wsgi script file).

Bill

On Tue, Feb 9, 2010 at 5:19 PM, Nick Mellor
<nick.mell...@pobox.com> wrote:

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> 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.
>
>

Reply all
Reply to author
Forward
0 new messages