Re: Handling a reusable form in multiple views
Perfect. I actually realized soon after posting that option 1 would work well for this scenario, but option 2 would be a good future enhancement. Thanks for your help. On Jun 29, 11:56 am, Rajesh Dhawan wrote: > delinowrote: > > Rajesh, > > > Thanks for your response. If I post the form to a dedicated view that > > works fine, but how do I handle validation properly in this approach. > > Say the user enters invalid data, do I now need to redirect them back > > to the original page and then display these errors? That seems like it > > will be a headache as well. Note that this form is a reusable form > > that gets displayed within most templates so it's just a little > > section of the page. If I now enter invalid data and post the form, > > it's going to post to a different view that will then see the form is > > invalid and then what?? > > Option 1: Send your invalid form to a simple template that knows how > to display the form and its errors. > > Option 2: Post your form via AJAX -- you should still implement Option > 1 for people that have turned off Javascript > > -RD --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Handling a reusable form in multiple views
delino wrote: > Rajesh, > > Thanks for your response. If I post the form to a dedicated view that > works fine, but how do I handle validation properly in this approach. > Say the user enters invalid data, do I now need to redirect them back > to the original page and then display these errors? That seems like it > will be a headache as well. Note that this form is a reusable form > that gets displayed within most templates so it's just a little > section of the page. If I now enter invalid data and post the form, > it's going to post to a different view that will then see the form is > invalid and then what?? Option 1: Send your invalid form to a simple template that knows how to display the form and its errors. Option 2: Post your form via AJAX -- you should still implement Option 1 for people that have turned off Javascript -RD --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Handling a reusable form in multiple views
Rajesh, Thanks for your response. If I post the form to a dedicated view that works fine, but how do I handle validation properly in this approach. Say the user enters invalid data, do I now need to redirect them back to the original page and then display these errors? That seems like it will be a headache as well. Note that this form is a reusable form that gets displayed within most templates so it's just a little section of the page. If I now enter invalid data and post the form, it's going to post to a different view that will then see the form is invalid and then what?? Thanks, Delino On Jun 25, 5:02 pm, Rajesh D wrote: > On Jun 25, 4:43 pm,delino wrote: > > > > > Hi, > > > I have a simple scenario using Django 0.96 and am looking for a > > cleaner solution than what I currently have: > > > I want to include a simple submission form (a few simple fields) on > > most pages of my site. I have the form action set to '.' so that > > validation can be done in place and errors displayed to the user. I am > > currently checking for the POST and either handling it or creating a > > new form in each of these views and then passing the form to each of > > my templates, e.g. > > > def example_page(request): > > if request.method == 'POST': > > form = SubmitMeForm(request.POST) > > if form.is_valid(): > > # do your thing > > return HttpResponseRedirect('/done/') > > else: > > form = SubmitMeForm() > > > variables = RequestContext(request, { > > 'submitMeForm': form, > > }) > > return render_to_response('somePage.html', variables) > > > I have 2 issues with this: > > > 1 - I have to include this code in each and every view that represents > > a page that has this template. There has to be a simpler way of doing > > it. > > > 2 - What if one of these pages has another Django form in it? I now > > have to handle the fact that the POST could have resulted from this > > form OR the other one. How would I do that? > > You could add your form to your request context using a context > processor under a key other than 'form' (so it doesn't clash with > forms defined by your other views). This way, your context processor > will make this form available globally. Secondly, instead of posting > this common form to '.', post it to a dedicated view that only knows > how to process this common form. > > -RD > > > > > TIA > > --delino --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Handling a reusable form in multiple views
On Jun 25, 4:43 pm, delino wrote: > Hi, > > I have a simple scenario using Django 0.96 and am looking for a > cleaner solution than what I currently have: > > I want to include a simple submission form (a few simple fields) on > most pages of my site. I have the form action set to '.' so that > validation can be done in place and errors displayed to the user. I am > currently checking for the POST and either handling it or creating a > new form in each of these views and then passing the form to each of > my templates, e.g. > > def example_page(request): > if request.method == 'POST': > form = SubmitMeForm(request.POST) > if form.is_valid(): > # do your thing > return HttpResponseRedirect('/done/') > else: > form = SubmitMeForm() > > variables = RequestContext(request, { > 'submitMeForm': form, > }) > return render_to_response('somePage.html', variables) > > I have 2 issues with this: > > 1 - I have to include this code in each and every view that represents > a page that has this template. There has to be a simpler way of doing > it. > > 2 - What if one of these pages has another Django form in it? I now > have to handle the fact that the POST could have resulted from this > form OR the other one. How would I do that? You could add your form to your request context using a context processor under a key other than 'form' (so it doesn't clash with forms defined by your other views). This way, your context processor will make this form available globally. Secondly, instead of posting this common form to '.', post it to a dedicated view that only knows how to process this common form. -RD > > TIA > -- delino --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Handling a reusable form in multiple views
Hi, I have a simple scenario using Django 0.96 and am looking for a cleaner solution than what I currently have: I want to include a simple submission form (a few simple fields) on most pages of my site. I have the form action set to '.' so that validation can be done in place and errors displayed to the user. I am currently checking for the POST and either handling it or creating a new form in each of these views and then passing the form to each of my templates, e.g. def example_page(request): if request.method == 'POST': form = SubmitMeForm(request.POST) if form.is_valid(): # do your thing return HttpResponseRedirect('/done/') else: form = SubmitMeForm() variables = RequestContext(request, { 'submitMeForm': form, }) return render_to_response('somePage.html', variables) I have 2 issues with this: 1 - I have to include this code in each and every view that represents a page that has this template. There has to be a simpler way of doing it. 2 - What if one of these pages has another Django form in it? I now have to handle the fact that the POST could have resulted from this form OR the other one. How would I do that? TIA -- delino --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---