1

I am expanding on the basic django Poll site tutorial, and I have made a view that allows users to add their own polls. Adding a poll works, adding choices does not. Apparently this is because the poll does not "exist" yet, and the p.id cannot be used. However, the p.id works when redirecting the browser at the bottom. Anyy ideas?

def save(request):
    p = Poll(question=request.POST['question'], pub_date=timezone.now())
    p.save()
    c1 = Choice(poll=p.id, choice_text=request.POST['c1'], votes=0)
    c2 = Choice(poll=p.id, choice_text=request.POST['c2'], votes=0)
    c3 = Choice(poll=p.id, choice_text=request.POST['c3'], votes=0)
    c4 = Choice(poll=p.id, choice_text=request.POST['c4'], votes=0)
    c1.save()
    c2.save()
    c3.save()
    c4.save()
    return HttpResponseRedirect(reverse('detail', args=(p.id,)))

1 Answer 1

1

Nevermind, I figured it out. The choice doent need an id, rather, it needs the object. FIxed by changing:

c1 = Choice(poll=p.id, choice_text=request.POST['c1'], votes=0)

to

c1 = Choice(poll=p, choice_text=request.POST['c1'], votes=0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.