views:

110

answers:

1

I've been using CakePHP for a long time now and feel comfortable developing sites with it. I do however like Python more then PHP and would like to move to Django.

EDIT: split in three separate questions

How can I mix multiple models in one form? I know formsets are used for this, but I can't find a decent tutorial on this (views + template). In Cake I can simply to this in my view (template):

echo $this->Form->input('User.title');
echo $this->Form->input('Profile.website');
echo $this->Form->input('Comment.0.title');
echo $this->Form->input('Comment.1.title');

This would mix a User model, a Profile model and add two comments in one form. How to do this with Django?

Thanks!

+2  A: 

In regards to part 1, this is much easier than you may think:

forms.py:

class UserForm(forms.ModelForm):
    pass ## Using pass so I don't have to write the whole thing.

class ProfileForm(forms.ModelForm):
    pass

class CommentForm(forms.ModelForm):
    pass

views.py:

def view_forms(request):
    userform = UserForm()
    profileform = ProfileForm()
    comment1 = CommentForm()
    comment2 = CommentForm()
    if request.method = "POST":
        ## Process forms here.  Yes, I'm lazy.
    return render_to_response("template.html",
                locals(),                       
                context_instance=RequestContext(request))

template.html:

<form method="POST">
    {{ userform.as_p }}
    {{ profileform.as_p }}
    {{ comment1.as_p }}
    {{ comment2.as_p }}
</form>
Jack M.
Wow, that IS easy, thanks!
IvanBernat