I don't care if the page reloads, how do I change the form?
On Sat, Jul 13, 2013 at 1:19 AM, Babatunde Akinyanmi <tundeba...@gmail.com>wrote: > 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-users@googlegroups.com > Subject: Re: Submit a form but stay on the page and the form changes > > 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. > > > On Fri, Jul 12, 2013 at 10:43 AM, Sergiy Khohlov <skhoh...@gmail.com>wrote: > >> 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 >> >> >> On Thu, Jul 11, 2013 at 11:50 PM, Cody Scott <cody.j.b.sc...@gmail.com>wrote: >> >>> 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. >>> >>> >>> >> >> -- >> 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+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. >> >> >> > > -- > 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. > > > > -- > 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+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. > > > -- 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.