Skip to content Skip to sidebar Skip to footer

Django Def Form_valid With Two Forms

I'd like to use two forms into the same views. It is a restricted channel. The first forms is a chat and the second one launch the conversation (boolean fiels with default = false)

Solution 1:

You can do it by adding a name to identify the submit button of the form submitted.

So your markup would look something along the lines of;


<form action="" method="post">
    {{ form_1 }}
    <input type="submit" name="form_1" value="Submit" />
</form>

<form action="" method="post">
    {{ form_2 }}
    <input type="submit" name="form_2" value="Submit" />
</form>

Then you can figure out which form was submitted in the post method;


    def post(self, *args, **kwargs):
        
        if 'form_1' in request.POST:
            process_form_1()
        elif 'form_2' in request.POST:
            process_form_2()

Post a Comment for "Django Def Form_valid With Two Forms"