5

Using:

Hi, I'm a python beginner, coming from a PHP background, so I apologize if this is a stupid question. I'm getting stuck when trying to call the p.was_published_today(). It outputs this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/path/to/mysite/polls/models.py", line 12, in was_published_today
    pub_date = models.DateTimeField('date published')
NameError: global name 'datetime' is not defined

But the code in my models.py looks (to me) exactly as I should have it according to the tutorial:

from django.db import models 
import datetime

# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

# other code but not relevant to the error

I've seen others around here asking about a very very similar issue with the datetime not working in this tutorial, but none of the answers to them actually helped me get it working. It works in the python interpreter but not in the script. I'm very confused & I've been working on this detail for 45 minutes. Does anybody have a clue?

Forrest
  • 149
  • 1
  • 2
  • 13

4 Answers4

14

Make sure you import datetime in your view. Add:

import datetime

to your Views.py page. There was a ticket that was once opened for this issue:

https://code.djangoproject.com/ticket/5668

rolling stone
  • 12,668
  • 9
  • 45
  • 63
  • 2
    That's actually his models.py page, the error indicates that the datetime module has not been imported. – rolling stone Jul 25 '11 at 01:04
  • 4
    Just to emphasise: the problem is that he needs to import `datetime` in the **view** as well. – Daniel Roseman Jul 25 '11 at 08:51
  • 1
    Thanks, this didn't fix the problem I was having, but your mention of editing views.py gave me an idea. I added `from datetime import datetime` into views.py, while keeping the `import datetime` in the models.py. Then I quit the shell & re-entered it, then it all worked. I've no clue why, but I'm sure it'll make more sense as I learn Python. You rawwwk dude! :-) – Forrest Jul 25 '11 at 15:35
6

You can use this one: Go through the link Django Utils Timezone

from django.utils.timezone you can import is_aware, is_naive, now and can customise based on your requirements:

Timezone-related classes and functions.

This module uses pytz when it's available and fallbacks when it isn't.

from django.utils.timezone import datetime
Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
5

Try the following:

from datetime import datetime

Then your code should work. datetime is the package name and inside it is the datetime you want to work with.

Evan Porter
  • 2,987
  • 3
  • 32
  • 44
  • This will not work, as `date` is an attribute on `datetime`, not `datetime.datetime`! – Bernhard Vallant Jul 25 '11 at 08:08
  • 1
    You're completely right. I need to learn to read. Looking at the traceback more carefully reveals that it makes little sense, since `datetime` is already imported. – Evan Porter Jul 25 '11 at 12:04
  • Wow, that worked when I added it to the views.py but not when I put it in the models.py. When it's in models.py, I got an error saying `AttributeError: 'method_descriptor' object has no attribute 'today'`. Anyway, I got to work thanks. The tutorial really should mention that or be fixed, 'cause I never would've guessed I needed to edit the views.py file TOO, to get it working. You rawwwk dude! :-) – Forrest Jul 25 '11 at 15:34
0

from datetime import datetime

This is the right way to import datetime in django. This is for Ubuntu 18.04 or higher I have 20.04.

Proud
  • 88
  • 7