I would like to put a Poll Voting Form on any page like a blog post page or 
the homepage.
Here is the page that I display the form and the view that it gets posted 
to in the form action attribute.
I would like to be able to submit the form but stay on that page and have 
the form change into the results or a link to the results.

class DetailView(PublishedMixin, generic.DetailView):
    model = Poll
    template_name = 'polls/detail.html'

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        context['form'] = PollForm(instance=self.get_object())
        return context
<form action="{% url 'polls:vote' poll.id %}" method="post">{% csrf_token 
%}{{form.as_p}}<input type="submit" value="Vote" /></form>


class VoteView(PublishedMixin, generic.DetailView):
    model = Poll

    '''    def get_form(self, *args, **kwargs):        form = 
PollForm(instance=self.get_object())        #return super(VoteView, 
self).get_form(*args, **kwargs)        return form    '''
    @property
    def success_url(self):
        return reverse(
            'polls:results', args=(self.get_object().id,))

    def post(self, request, *args, **kwargs):
        form = PollForm(request.POST, instance=self.get_object())
        if form.is_valid():
            selected_choice = form.cleaned_data['choice']
            selected_choice.votes += 1
            selected_choice.save()      


            return HttpResponseRedirect(request.META['HTTP_REFERER'])






I also thought about doing a mixin on the page, but I can't just render the 
page with the form attribute equal to something else, becuase I would need to 
redirect to avoid the refresh problem.



class PollFormMixin(SingleObjectMixin):    """    puts form and "view results 
link" in context    and validates and saves the form    """    model = Poll    
def get_context_data(self, **kwargs):        context = super(PollFormMixin, 
self).get_context_data(**kwargs)        form = 
PollForm(instance=self.get_object())        context['form'] = form        
return context    def post(self, request, *args, **kwargs):        form = 
PollForm(request.POST, instance=self.get_object())        if form.is_valid():   
         form.save()            return HttpResponseRedirect(self.success_url)   
     else:            return render(request, self.template_name,                
{'form': form, 'poll': self.get_object()})


class VoteView(PublishedPollMixin, PollFormMixin, DetailView):    """    Vote 
on a poll    """    model = Poll    template_name = 'polls/detail.html'    
@property    def success_url(self):        return reverse(            
'polls:results', args=(self.get_object().id,))

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to