Django import datetime
Asked Answered
B

4

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?

Biogeochemistry answered 25/7, 2011 at 0:11 Comment(0)
R
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

Root answered 25/7, 2011 at 0:28 Comment(3)
That's actually his models.py page, the error indicates that the datetime module has not been imported.Root
Just to emphasise: the problem is that he needs to import datetime in the view as well.Devinna
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! :-)Biogeochemistry
V
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
Vitavitaceous answered 2/6, 2016 at 15:52 Comment(0)
S
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.

Stradivari answered 25/7, 2011 at 4:13 Comment(3)
This will not work, as date is an attribute on datetime, not datetime.datetime!Taoism
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.Stradivari
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! :-)Biogeochemistry
H
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.

Holladay answered 19/7, 2020 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.