Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Problem in Django App Part 1 Tutorial [RSS Feed]

#1 June 16, 2010 13:20:12

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


Hello,
I am a Python newbie and this is my first time I use Django and
Python.
I was reading the Django App tutorial Part 1 and got stuck in a place.

I execute these statements in the Python Shell:

>>> from mysite.polls.models import Poll, Choice
>>> import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
>>> p.save()
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_today()

For the last statement I get an error:

Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_today'


This is my "models.py" file:

from django.db import models
import datetime

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()

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.question


I wrote it with IDLE.
I thought it was an indentation and whitespace problem, but I followed
strictly the identation rules and used only tabs.
Also I tried other editors and nothing changed. the error message
still shows up.
Please help me, what am I doing wrong?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 16, 2010 15:09:22

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


Never mind I solved the problem!
For all of you with the same problem: it appears that the first time
that you use or import (i'm not really sure) a class in the python
shell, it's source-code file is compiled automatically in a ".pyc"
file, in the same directory where the source-code file is located and
with the same name.
After that everytime you import that class, the python interpreter
gets it from the ".pyc" file. You added the "was_published_today()"
method to the Poll class in the "models.py" file, but the "models.pyc"
file still contains the old version even after an re-import or reuse
of the Poll class. It seems that Python compiles the module only the
first time it is imported, and after that even if the source-code in
"models.py" changes, the python interpreter doesn't automatically
recompile the new version of the code. That's why the ".pyc" file
still has the old version of the Poll class and that's where the
Python interpreter imports the class from so you get the
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_today'
error in the interpreter.
I think that it has nothing to do with indentation or whitespaces. If
you delete the ".pyc" files for every file you change, then there
should be no more problems. Just go to polls/ directory and delete
"models.pyc" (or any other ".pyc" file if you had made changes to
"settings.py" or "urls.py").
Everything should work fine now.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 June 16, 2010 15:09:56

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


Never mind I solved the problem!
For all of you with the same problem: it appears that the first time
that you use or import (i'm not really sure) a class in the python
shell, it's source-code file is compiled automatically in a ".pyc"
file, in the same directory where the source-code file is located and
with the same name.
After that everytime you import that class, the python interpreter
gets it from the ".pyc" file. You added the "was_published_today()"
method to the Poll class in the "models.py" file, but the "models.pyc"
file still contains the old version even after an re-import or reuse
of the Poll class. It seems that Python compiles the module only the
first time it is imported, and after that even if the source-code in
"models.py" changes, the python interpreter doesn't automatically
recompile the new version of the code. That's why the ".pyc" file
still has the old version of the Poll class and that's where the
Python interpreter imports the class from so you get the
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_today'
error in the interpreter.
I think that it has nothing to do with indentation or whitespaces. If
you delete the ".pyc" files for every file you change, then there
should be no more problems. Just go to polls/ directory and delete
"models.pyc" (or any other ".pyc" file if you had made changes to
"settings.py" or "urls.py").
Everything should work fine now.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 16, 2010 15:10:29

T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


I noticed you are importing your project name, are you invoking your
"python manage.py shell" from within your project folder? If you are,
try the following:

project_folder/python manage.py shell
>>> from polls.models import Poll
>>> dir(Poll)

The above should show you that "was_published_today" method in a
list. Try to call the method again:

>>> Poll.objects.get(pk=1).was_published_today()


On Jun 16, 8:08 am, "!...@!!!" <eltoni...@gmail.com> wrote:
> Hello,
> I am a Python newbie and this is my first time I use Django and
> Python.
> I was reading the Django App tutorial Part 1 and got stuck in a place.
>
> I execute these statements in the Python Shell:
>
> >>> from mysite.polls.models import Poll, Choice
> >>> import datetime
> >>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())
> >>> p.save()
> >>> p = Poll.objects.get(pk=1)
> >>> p.was_published_today()
>
> For the last statement I get an error:
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
> AttributeError: 'Poll' object has no attribute 'was_published_today'
>
> This is my "models.py" file:
>
> from django.db import models
> import datetime
>
> 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()
>
> class Choice(models.Model):
>     poll = models.ForeignKey(Poll)
>     choice = models.CharField(max_length=200)
>     votes = models.IntegerField()
>     def __unicode__(self):
>         return self.question
>
> I wrote it with IDLE.
> I thought it was an indentation and whitespace problem, but I followed
> strictly the identation rules and used only tabs.
> Also I tried other editors and nothing changed. the error message
> still shows up.
> Please help me, what am I doing wrong?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 16, 2010 17:50:01

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


Thanks Thierry, but problem solved!...:)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 June 16, 2010 17:55:46

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


P

On Jun 16, 6:49 pm, "!...@!!!" <eltoni...@gmail.com> wrote:
> Thanks Thierry, but problem solved!...:)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 June 16, 2010 18:06:22

!.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem in Django App Part 1 Tutorial


PO

On Jun 16, 6:55 pm, "!...@!!!" <eltoni...@gmail.com> wrote:
> P
>
> On Jun 16, 6:49 pm, "!...@!!!" <eltoni...@gmail.com> wrote:
>
>
>
> > Thanks Thierry, but problem solved!...:)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Problem in Django App Part 1 Tutorial [RSS Feed]

Board footer

Moderator control

Enjoy the 13th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support