OK here's the entire views.py code:

# Create your views here.


from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404,
get_list_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from polls.models import Choice, Poll
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
import datetime
from django import forms
from polls.forms import AddChoiceForm


def index(request):
    #1 - return HttpResponse("Hello, World. You are at the poll
index.")
    #2 - latest_poll_list = Poll.objects.all().order_by('-pub_date')[:
5]
    #2 - output = ', '.join([p.question for p in latest_poll_list])
    #2 - return HttpResponse(output)
    #3 - latest_poll_list = Poll.objects.all().order_by('-pub_date')[:
5]
    #3 - t = loader.get_template('polls/index.html')
    #3 - c = Context({
    #3 -     'latest_poll_list': latest_poll_list,
    #3 - })
    #3 - return HttpResponse(t.render(c))
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    return render_to_response('polls/index.html', {'latest_poll_list':
latest_poll_list})


def detail(request, poll_id):
    #1 - return HttpResponse("You're looking at poll %s." % poll_id)
    #2 - try:
    #2 -     p = Poll.objects.get(pk=poll_id)
    #2 - except Poll.DoesNotExist:
    #2 -     raise Http404
    #2 - return render_to_response('polls/detail.html', {'poll': p})
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('polls/detail.html', {'poll': p},
context_instance=RequestContext(request))


def results(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('polls/results.html', {'poll':p})

def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # redisplay the poll voting form.
        return render_to_response('polls/detail.html', {
            'poll':p,
            'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully
dealing
        # with POST data. This prevents data from being posted twice
if a
        # user hits the back button.
        return HttpResponseRedirect(reverse('polls.views.results',
args=(p.id,)))

def pollvotecount(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    choices = get_list_or_404(Choice, poll=poll_id)
    dctnry = {'poll': p}
    totalvotes = 0
    choiceList = []
    for choice in choices:
        choiceList.append(choice)
        totalvotes += choice.votes

    dctnry['clist'] = choiceList
    dctnry['total'] = totalvotes
    return render_to_response('polls/pollvotecount.html', dctnry)

def choicecount(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('polls/choicecount.html', {'poll':
p})


def summary(request):
    p = get_list_or_404(Poll)
    dctnry = {'poll': p}
    pollcount = Poll.objects.all().count()
    dctnry['pcount'] = pollcount
    return render_to_response('polls/summary.html', dctnry)


def addpoll(request):
    dctnry = {}
    pollcount = 0

    def countpolls():
        pollcount = Poll.objects.all().count()
        dctnry['pcount'] = pollcount

    try:
        newquestion = ""

        if request.method == 'POST':
            newquestion = request.POST['question']
    except():
        countpolls()
        dctnry['message'] = "No new poll data added"

        return render_to_response('polls/addpoll.html', dctnry,
 
context_instance=RequestContext(request))
    else:
        if newquestion != "":
            newpoll = Poll(question=newquestion,
pub_date=datetime.date.today())
            newpoll.save()

        countpolls()
        dctnry['newquestion'] = newquestion
        dctnry['message'] = "New poll question was added"
        return render_to_response('polls/addpoll.html', dctnry,
 
context_instance=RequestContext(request))


def addchoice(request):
    polls = get_list_or_404(Poll)
    data = {'polls': polls}
    choiceform = AddChoiceForm(data)
    return render_to_response('polls/addchoice.html', {
                              'choiceform': choiceform,
    })


At this point I just want to display the addchoice screen, so that's
why I've coded the addchoice view the way it is. I'm not seeing
anything obvious so far that's referencing a form that's not being
imported, since this is the first form that I've coded, imported and
referenced. Still looking. Thanks for the help.


On Jan 5, 11:20 am, Dan Fairs <dan.fa...@gmail.com> wrote:
> Hi,
>
> > Near the top of my views.py I do have "from django import forms", so
> > I'm guessing that based on previous comments that this is ok. Do I
> > also need to import my specific named-form which IO created for the
> > new screen/view from my polls.forms.py module? If so is this syntax
> > correct:
>
> > from polls.forms import AddChoiceForm
>
> > If not let me know. Thanks.
>
> From your original code - yes, you'll need that line you specify above.
>
> However, the original error you posted was referring to something called 
> 'form' which wasn't defined. Check through your code in views.py for any 
> reference to 'form'. You'll also need to check any code you've written that's 
> imported by your views.py for references to a 'form' that hasn't been defined 
> - that will also cause views.py to fail to import.
>
> Cheers,
> Dan
> --
> Dan Fairs | dan.fa...@gmail.com |www.fezconsulting.com

-- 
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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to