Masklinn wrote:
> On 29 Oct 2008, at 15:06 , Paddy Joy wrote:
> > Is there an easy way of doing this or do I need to create a custom
> > form that contains the fields of both models and then separate the
> > data in my view?
> >
> > Paddy
>
> What's wrong with creating two different Forms (one for the account,
> one for the user) and displaying them together in the template? Django
> has no problem with that (it's in fact one of Django's strengths,
> being able to mix and match several Forms in a single view&template).
Thanks I didn't know I could do that. For some reason I was fixated on
the concept of one form/one view/one template.
I tried as you suggested and it turned out to be pretty straight
forward.
view.py:
@login_required
def CreateAccount(request):
if request.method == "POST":
AccountForm = NewAccountForm(request.POST)
UserForm = CreateUserForm(request.POST)
if UserForm.is_valid() & AccountForm.is_valid():
newuser = UserForm.save()
newaccount = AccountForm.save(commit=False)
newaccount.owner = newuser
newaccount.save()
return HttpResponseRedirect("/account/")
else:
AccountForm = NewAccountForm()
UserForm = CreateUserForm()
return render_to_response('test.html', {"form1": AccountForm,
"form2": UserForm, }, context_instance=RequestContext(request))
test.html:
{% extends "base.html" %}
{% block content %}
<form method="POST" action="">
<table>
{{ form1.as_table }}
{{ form2.as_table }}
</table>
<input type="submit" value="Submit" />
</form>
{% endblock %}
Paddy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---