Submit a form but stay on the page and the form changes

585 views
Skip to first unread message

Cody Scott

unread,
Jul 11, 2013, 4:50:14 PM7/11/13
to django...@googlegroups.com
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,))

Sergiy Khohlov

unread,
Jul 12, 2013, 10:43:45 AM7/12/13
to django-users
Hello Cody, 

 I would like to help. 
 1 ) could you please check if POST is going from  your browser to  django (I hope yes)
2)  First candidate for investigation is  function post in class PollFormMixin
 I 'm proposing verify this by  adding 
 print  "form status",  form.is_valid() 
 after string form = PollForm(request.POST, instance=self.get_object())


 P.S. How are you adding so nice formatting to your letter ?


Many thanks,

Serge


+380 636150445
skype: skhohlov


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Cody Scott

unread,
Jul 12, 2013, 6:51:37 PM7/12/13
to django...@googlegroups.com
The code works, I can submit the form and go back to the page I came from with the HTTP_REFERER , what I need help is getting the form to change, to be the results.

For the syntax highlighting I submit my code to bpaste and then copy into the message here.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/M_quXXXua_o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

Babatunde Akinyanmi

unread,
Jul 13, 2013, 1:19:20 AM7/13/13
to django...@googlegroups.com
I'm assuming you want the form to morph on the screen without a page reload. If that's the case, you need JavaScript like jQuery. let the view you are submitting your POST to return the data you need in JSON and use jQuery to remove the form and add the results or url

Sent from my Windows Phone

From: Cody Scott
Sent: 7/12/2013 11:51 PM
To: django...@googlegroups.com
Subject: Re: Submit a form but stay on the page and the form changes

Cody Scott

unread,
Jul 15, 2013, 4:37:16 PM7/15/13
to django...@googlegroups.com
I don't care if the page reloads, how do I change the form?

Bill Freeman

unread,
Jul 15, 2013, 4:49:05 PM7/15/13
to django-users
Render a different form

  or

Render the body tag with a different class or classes that hide some parts of your grander form and reveal others

  or

Render a script tag setting a value in a variable that your JavaScript uses to decide how to make the form look.


If you want to control how your page looks there's rarely a substitute for controlling how your page looks.
Reply all
Reply to author
Forward
0 new messages