Discussion:
Is it possible to display multiple forms in a single template file?
ayayalar
2008-11-20 22:03:03 UTC
Permalink
VIEW:

def add_product(request):
if not request.method == 'POST':
form1 = ProductForm()
return render_to_response('index.html', {'form1' : form1})
else:
form1 = ProductForm(request.POST)
if form1.is_valid():
form1.save()
return HttpResponseRedirect('/thanks.html')


def add_product_details(request):
if not request.method == 'POST':
form2 = ProductDetailForm()
return render_to_response('index.html', {'form2' : form2})
else:
if form2.is_valid():
form2.save()
return HttpResponseRedirect('/thanks.html')





URLS
urlpatterns = patterns('',

(r'^product/$', views.add_product),
(r'^product/$', views.add_product_details),


Is this possible? For some reason the 2nd form never shows up
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Brian Neal
2008-11-20 22:15:57 UTC
Permalink
        form1 = ProductForm()
        return render_to_response('index.html', {'form1' : form1})
        form1 = ProductForm(request.POST)
            form1.save()
            return HttpResponseRedirect('/thanks.html')
        form2 = ProductDetailForm()
        return render_to_response('index.html', {'form2' : form2})
            form2.save()
            return HttpResponseRedirect('/thanks.html')
URLS
urlpatterns = patterns('',
    (r'^product/$', views.add_product),
    (r'^product/$', views.add_product_details),
Is this possible? For some reason the 2nd form never shows up
Well you need to pass both forms to the template to get them both to
show up.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
ayayalar
2008-11-20 22:50:44 UTC
Permalink
And I do that. Here is my template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title><!-- Insert your title here --></title>
</head>
<body>
<form action="." method="POST">
<table>
{{ form1.as_table }}
</table>

<p><input type="submit" value="Submit"></p>
</form>
<form action="." method="POST">
<table>
{{ form2.as_table }}
</table>

<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
Post by Brian Neal
        form1 = ProductForm()
        return render_to_response('index.html', {'form1' : form1})
        form1 = ProductForm(request.POST)
            form1.save()
            return HttpResponseRedirect('/thanks.html')
        form2 = ProductDetailForm()
        return render_to_response('index.html', {'form2' : form2})
            form2.save()
            return HttpResponseRedirect('/thanks.html')
URLS
urlpatterns = patterns('',
    (r'^product/$', views.add_product),
    (r'^product/$', views.add_product_details),
Is this possible? For some reason the 2nd form never shows up
Well you need to pass both forms to the template to get them both to
show up.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Andy McKay
2008-11-20 22:57:20 UTC
Permalink
Yes but you never pass two forms to the template at the same time. In
add_product you pass form1, in add_product_details you pass form2.
Post by ayayalar
(r'^product/$', views.add_product),
(r'^product/$', views.add_product_details),
You have the same URL twice, only one of them will ever be active.

Remove one of those of URL's, then merge the two views together and
pass both form1 and form2 to the template.
--
Andy McKay
Clearwind Consulting

Site: www.clearwind.ca
Blog: www.agmweb.ca/blog/andy






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Brian Neal
2008-11-20 23:19:40 UTC
Permalink
No, you are not. Check the dictionary you are passing into your
render_to_response() calls again.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Karen Tracey
2008-11-20 22:57:54 UTC
Permalink
Post by ayayalar
form1 = ProductForm()
return render_to_response('index.html', {'form1' : form1})
form1 = ProductForm(request.POST)
form1.save()
return HttpResponseRedirect('/thanks.html')
form2 = ProductDetailForm()
return render_to_response('index.html', {'form2' : form2})
form2.save()
return HttpResponseRedirect('/thanks.html')
URLS
urlpatterns = patterns('',
(r'^product/$', views.add_product),
(r'^product/$', views.add_product_details),
Is this possible? For some reason the 2nd form never shows up
A request is going to match only one url pattern (assuming a match is
found). You seem to be expecting that one request will first match the
first (add_product) view you have specified and then when that is done the
2nd (add_product_details) one will get called. That's not how it works. If
you wont both the code that is in add_product and add_product_details to be
called for your r'^product/$' url, then you need to combine their code into
one function that you specify in a single urlentry for r^product/$'. In
that combined view you need to create both forms and pass both of them in
the context to your template.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Malcolm Tredinnick
2008-11-20 23:07:43 UTC
Permalink
On Thu, 2008-11-20 at 14:03 -0800, ayayalar wrote:
[...]
Post by ayayalar
URLS
urlpatterns = patterns('',
(r'^product/$', views.add_product),
(r'^product/$', views.add_product_details),
This has no chance of doing what you expect. Only one view function will
be called for a single request. If you want to pass two form objects to
the template, you need to do it from the same view function. You can
still put the form creation pieces in separate functions, but your view
function will have to call both of them and combine the results into a
context to pass to a template.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
ayayalar
2008-11-20 23:18:29 UTC
Permalink
I understand, I wish it was possible to bind multiple view functions
to a single template through the urls...

Would something like this possible?

URLS
urlpatterns = patterns('',

(r'^product/$', views.add_product, views.add_product_details),)
Post by Malcolm Tredinnick
[...]
Post by ayayalar
URLS
urlpatterns = patterns('',
    (r'^product/$', views.add_product),
    (r'^product/$', views.add_product_details),
This has no chance of doing what you expect. Only one view function will
be called for a single request. If you want to pass two form objects to
the template, you need to do it from the same view function. You can
still put the form creation pieces in separate functions, but your view
function will have to call both of them and combine the results into a
context to pass to a template.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
ayayalar
2008-11-20 23:23:48 UTC
Permalink
I am able to do this, but this is pure ugly. Not a very elegant way of
doing things.

def product(request):
if not request.method == 'POST':
form1 = ProductForm()
form2 = ProductDetailForm()
return render_to_response('index.html', {'form1' : form1,
'form2' : form2})
else:
form1 = ProductForm(request.POST)
form2 = ProductDetailForm(request.POST)
if form1.is_valid():
form1.save()
if form2.is_valid():
form2.save()
return HttpResponseRedirect('/thanks.html')
Post by ayayalar
I understand, I wish it was possible to bind multiple view functions
to a single template through the urls...
Would something like this possible?
URLS
urlpatterns = patterns('',
    (r'^product/$', views.add_product, views.add_product_details),)
Post by Malcolm Tredinnick
[...]
Post by ayayalar
URLS
urlpatterns = patterns('',
    (r'^product/$', views.add_product),
    (r'^product/$', views.add_product_details),
This has no chance of doing what you expect. Only one view function will
be called for a single request. If you want to pass two form objects to
the template, you need to do it from the same view function. You can
still put the form creation pieces in separate functions, but your view
function will have to call both of them and combine the results into a
context to pass to a template.
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
ayayalar
2008-11-20 23:26:38 UTC
Permalink
Continue reading on narkive:
Loading...