Thursday, July 24, 2014

Problem in the Tutorial 01

Hey guys,

I'm new with Django and Python as well. I'm doing the tutorial 1 on the website (https://docs.djangoproject.com/en/1.6/intro/tutorial01/).
I'm using Python27 (C:\Python27)
In C:\Python27\Scripts\ I have the project mysite and polls like in the tutorial

In the part "Playing with the API"

c:\Python27\Scripts\mysite

>manage.py syncdb
Creating tables ...
Creating table polls_poll
Creating table polls_choice
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

c:\Python27\Scripts\mysite>manage.py shell
Python 2.7.7 (default, Jun  1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
[]
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
>>> p.save()
>>> p.id
1
>>> p.question
"What's new?"
>>> p.pub_date
datetime.datetime(2014, 7, 24, 11, 10, 43, 935000, tzinfo=<UTC>)
>>> p.question = "What's up?"
>>> p.save()
>>> Poll.objects.all()
[<Poll: Poll object>]
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: Poll object>]
>>> Poll.objects.filter(id=1)
[<Poll: Poll object>]
>>> Poll.objects.all()
[<Poll: Poll object>]
>>> Poll.objects.filter(id=1)
[<Poll: Poll object>]
>>> Poll.objects.filter(question__startswith='What')
[<Poll: Poll object>]
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Poll.objects.get(pub_date__year=current_year)
<Poll: Poll object>
>>> Poll.objects.get(id=2)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 151, in
 get
    return self.get_queryset().get(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 310, in g
et
    self.model._meta.object_name)
DoesNotExist: Poll matching query does not exist.
>>> Poll.objects.get(pk=1)
<Poll: Poll object>
>>> p = Poll.objects.get(pk=1)
>>> o.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'o' is not defined
>>> p.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_recently'
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_recently'
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: Poll object>]
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
>>> p.save()
>>> p.id
2
>>> p.question
"What's new?"
>>> p.pub_date
datetime.datetime(2014, 7, 24, 11, 54, 15, 453000, tzinfo=<UTC>)
>>> p.question = "What's up?"
>>> p.save()
>>> Poll.objects.all()
[<Poll: Poll object>, <Poll: Poll object>]
>>> from polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: Poll object>, <Poll: Poll object>]
>>> Poll.objects.filter(id=1)
[<Poll: Poll object>]
>>> Poll.objects.filter(question__startswith='What')
[<Poll: Poll object>, <Poll: Poll object>]
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Poll.objects.get(pub_date__year=current_year)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 151, in
 get
    return self.get_queryset().get(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 313, in g
et
    (self.model._meta.object_name, num))
MultipleObjectsReturned: get() returned more than one Poll -- it returned 2!
>>> Poll.objects.get(id=2)
<Poll: Poll object>
>>> Poll.objects.get(pk=1)
<Poll: Poll object>
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Poll' object has no attribute 'was_published_recently'
>>> p = Poll.objects.get(pk=1)
>>> p.choice_set.all()
[]
>>> p.choice_set.create(choice_text='Not much', votes=0)
<Choice: Choice object>
>>> p.choice_set.create(choice_text="The Sky", votes=0)
<Choice: Choice object>
>>> c = p.choice_set.create(choice_text="Just hacking again", votes=0)
>>> c.poll
<Poll: Poll object>
>>> p.choice_set.all()
[<Choice: Choice object>, <Choice: Choice object>, <Choice: Choice object>, <Cho
ice: Choice object>, <Choice: Choice object>, <Choice: Choice object>]
>>> p.choice_set.count()
6

I get always Poll: Poll object or Choice: Choice object instead of the defined text. Also I have problems with the part of

p = Poll.objects.get(pk=1)
>>> p.was_published_recently()

Could somebody help me please? Before I start with the tutorial number 2...

Thanks a lot.

Fabian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f67739a9-d300-463a-8ea3-6c8b2d3a2a14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment