Edit django form containing modelforms and inline formset factory

602 views Asked by At

I am working in Django 1.5. I have a task submitting a form and editting it. My form contains order details, customer details, purchased product details, attachments and comments. Order details model have customer foreign key, product details have order foreign key. I am using modelform objects of order details,customer details,attachments and comments and inlineformset_factory object for products. I have successfully inserted data using this 'multi-object' form. Now I would like to edit this form. All these form objects have to be passed to the template. Please help me. Thanks in advance.

1

There are 1 answers

0
Jon On

With an existing template it's very easy to create your forms. Just add your specific forms to your template like this example below:

<form action="{% url 'changeprofile' %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
            {{ user_form }}
            {{ location_form }}
            {{ user_profile_form }}
            <input type="submit" value="Submit" />
</form>

Of course your view needs to instantiate them correctly.

    from django import shortcuts
    ...
    user_form = UserForm
    user_profile_form = UserProfileForm2(initial=inital_form_values)
    location_form = LocationForm

    forms = {'user_form':user_form,
             'user_profile_form':user_profile_form,
             'location_form':location_form}
    return shortcuts.render(request, 'changeprofile.html', forms)

The enctype="multipart/form-data" you only need if you have data like files or images.