Add poll to the django tutorial polls

39 views
Skip to first unread message

Christo bale

unread,
Apr 5, 2014, 12:43:04 PM4/5/14
to django...@googlegroups.com
I would like to make it possible to add a poll on the index.html, so one don't need the admin. 
I added a forms.py.
Right now when I click enter after typing the new poll, the sites goes blank and I need to refresh it. And no polls have been added. I am pretty new to django, so I hope that you can help me.  

forms.py
class ComposePoll(forms.ModelForm):
    class Meta:
        fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), 
        ]

index.html

<form action='' method='post'> {% csrf_token %}
    {{ form.add_poll }}
    <input class='btn btn-default' type='text'>
</form>

views.py
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_poll_list'

    def get_queryset(self):
        """
        Return the last five published polls (not including those set to be
        published in the future).
        """
        return Poll.objects.filter(
            pub_date__lte=timezone.now()
        ).order_by('-pub_date')[:5]
    
        add_poll = ComposePoll(request.POST or None)
        if add_poll.is_valid():
            create_poll = Poll.save(commit=False)
            create_poll.sent = datetime.datetime.now()
            create_poll.save()
            return HttpResponseRedirect('/polls/')
        
        poll = Poll.objects.filter()
        
        return render_to_response('polls/index.html', locals(), context_instance=RequestContext(request))

Camilo Torres

unread,
Apr 6, 2014, 1:25:50 PM4/6/14
to django...@googlegroups.com
Hello,

You should add a 'post' method to your view to get the data from the form and store it in your models.

Regards,

Camilo.
Reply all
Reply to author
Forward
0 new messages