Problem in the Tutorial 01

360 views
Skip to first unread message

Fabian Zentner

unread,
Jul 24, 2014, 8:53:54 AM7/24/14
to django...@googlegroups.com
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

Sergiy Khohlov

unread,
Jul 24, 2014, 10:23:18 AM7/24/14
to django-users

On Thu, Jul 24, 2014 at 3:53 PM, Fabian Zentner <zentner...@googlemail.com> wrote:
Poll.objects.get(pub_date__year=current_year)

try to use
Poll.objects.first()
 you have 2 object and it is a reason of your traceback

Many thanks,

Serge


+380 636150445
skype: skhohlov

Daniel Roseman

unread,
Jul 24, 2014, 10:41:01 AM7/24/14
to django...@googlegroups.com
On Thursday, 24 July 2014 13:53:54 UTC+1, Fabian Zentner wrote:
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"

<snip>
 
>>> 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


After you make changes to the code,  you need to reload it: the easiest way is exit your shell and go back in.
-- 
DR.

Ricardo Daniel Quiroga

unread,
Jul 27, 2014, 1:38:09 PM7/27/14
to django...@googlegroups.com
get() si no existe coincidencia te tira error asi de simple.


--
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...@googlegroups.com.
To post to this group, send email to django...@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/d80dc547-6194-4398-81d9-e93db01ca9cf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
------------------------------------------------------------
Ricardo Daniel Quiroga - L2Radamanthys

   Msn: l2rada...@gmail.com
           ricard...@hotmail.com

   Email: l2rada...@gmail.com
           l2rada...@saltalug.org.ar
           ricardoqu...@gmail.com
 
   sitio Web: http://www.l2radamanthys.com.ar
                   http://github.com/L2Radamanthys
   Twitter:    @l2Radamanthys
---------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages