Going throught the Django tutorial help pls

62 views
Skip to first unread message

Michael Carey

unread,
Aug 1, 2014, 9:44:46 PM8/1/14
to django...@googlegroups.com
I have:

Enter code here...from django.shortcuts import render
from django.template import RequestContext, loader
from django.http import HttpResponse

from polls.models import Poll

def index(request):
    latest_poll_list
= Poll.objects.get(name="Test") #Poll.objects.all().order_by('-pub_date')[:5]    context = {'latest_poll_list': latest_poll_list}
   
return render(request, 'polls/index.html', context)

def detail(request, poll_id):
   
try:
        poll
= Poll.objects.get(pk=poll_id)
   
except Poll.DoesNotExist:
       
raise Http404
   
return render(request, 'polls/detail.html', {'poll': poll})

def results(request, poll_id):
   
return HttpResponse("You're looking at the results of poll %s." % poll_id)

def vote(request, poll_id):
   
return HttpResponse("You're voting on poll %s." % poll_id)

It currently shows a list of the polls, what I am trying to work out is how to print out the integer value that is associated with that pole.

latest_poll_list = Poll.objects.get(name="Test")


Thankyou

MC

Collin Anderson

unread,
Aug 1, 2014, 10:55:25 PM8/1/14
to django...@googlegroups.com
It currently shows a list of the polls, what I am trying to work out is how to print out the integer value that is associated with that pole.

Is poll.id what you want? In the template it would be {{ poll.id }}

Or: Poll.objects.get(name="Test").id

Michael Carey

unread,
Aug 2, 2014, 12:12:00 AM8/2/14
to django...@googlegroups.com

So I tried:

def index(request):
    latest_poll_list
= Poll.objects.get(name="Test").id #Poll.objects.all().order_by('-pub_date')[:5]  
    context
= {'latest_poll_list': latest_poll_list}

   
return render(request, 'polls/index.html', context)

 and get a error, sorry I am really new to both python and SQLite

I do have a poll "Test"

Thank you

Mick C

Collin Anderson

unread,
Aug 2, 2014, 10:27:53 AM8/2/14
to django...@googlegroups.com
What does the error say?

Michael Carey

unread,
Aug 2, 2014, 3:23:31 PM8/2/14
to django...@googlegroups.com

FieldError at /polls/


Cannot resolve keyword 'name' into field. Choices are: choice, id, pub_date, question
   

   
     
     
   


   
     
     
   


   
     
     
   

   
     
     
   
   
     
     
   
   
     
     
   
   
     
     
Request Method:GET
Request URL:http://127.0.0.1:8000/polls/
Django Version:1.6.5
Exception Type:FieldError
Exception Value:
Cannot resolve keyword 'name' into field. Choices are: choice, id, pub_date, question
Exception Location:/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py in names_to_path, line 1283
Python Executable:/usr/bin/python
Python Version:2.7.6
Python Path:
['/home/michael/workspace/new/mysite',
 
'/usr/lib/python2.7',
 
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
 
'/usr/lib/python2.7/lib-tk',
 
'/usr/lib/python2.7/lib-old',
 
'/usr/lib/python2.7/lib-dynload',
 
'/usr/local/lib/python2.7/dist-packages',
 
'/usr/lib/python2.7/dist-packages',
 
'/usr/lib/python2.7/dist-packages/PILcompat',
 
'/usr/lib/python2.7/dist-packages/gst-0.10',
 
'/usr/lib/python2.7/dist-packages/gtk-2.0',
 
'/usr/lib/pymodules/python2.7',
 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:Sat, 2 Aug 2014 19:20:54 +0000

Thank you

Michael C

On Sunday, 3 August 2014 00:27:53 UTC+10, Collin Anderson wrote:
What does the error say?

Collin Anderson

unread,
Aug 2, 2014, 11:28:56 PM8/2/14
to django...@googlegroups.com
Maybe try:

Poll.objects.get(question="Test")

Babatunde Akinyanmi

unread,
Aug 3, 2014, 11:51:37 AM8/3/14
to Django users

Perhaps you should show your model definition and the complete traceback.

A wild guess though, did you syncdb?

--
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/3af5d757-f76c-40ac-ab54-f2ff64aa69c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages