fpena06
fpena06

Reputation: 2426

Django NameError: name 'models' is not defined

I have a small application that I wrote in python. Did some research on some web frameworks and decided to use django. I'm reading through the manual and going step by step to learn as much, however I'm stuck on the example given on page 19. When I type the command an I get and error.

import datetime
from django.utils import timezone
# ...
class Poll(models.Model):
# ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

Error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'models' is not defined

I googled "Django NameError:" and didn't find much.

Thanks.

Upvotes: 2

Views: 28410

Answers (3)

DevB2F
DevB2F

Reputation: 5075

Got this error when accidentally using models.CharField in a form, instead of using forms.CharField

Upvotes: 1

Miguel Pereira
Miguel Pereira

Reputation: 1791

Another instance of this error appears when you miss spell stuff :) like Models.Model instead of models.Model. Quite annoying.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

You accidentally missed the whole import for models.

from django.db import models

Upvotes: 16

Related Questions