How do I create 2 forms simultaneously Django

0

What I want is in a single templante bring these 2 forms simultaneously, in separate pages works, but the two together still could not do. Do I need to do an intermediate form? Am I doing wrong at some point?

... forms.py

from django import forms
from .models import *


class IdiomaForm(forms.ModelForm):

    class Meta:
        model = Idioma
        fields = '__all__'


class LinguagemForm(forms.ModelForm):

    class Meta:
        model = Linguagem
        fields = '__all__'

.... views.py

def Idioma(request):
    form = IdiomaForm()
    return render(request, 'curriculo/idioma.html', {'form': form})

.... language.html

{

% extends 'curriculo/base.html' %}
{% block content %}

    <form action="" method="post">
        {% csrf_token %}
        {{ form }}
        <input type="submit" class="btn btn-info" value="Salvar">
    </form>

{% endblock content %}
    
asked by anonymous 19.12.2016 / 12:28

1 answer

0

The last context for the render function is a dictionary, you can pass as many arguments as you like and with the name that suits you. For example:

def Idioma(request):
    form_idioma = IdiomaForm()
    form_ling = LinguagemForm()
    return render(request, 'curriculo/idioma.html', {'form1': form_idioma, 'form2': form_ling})

This way you can have two forms in the same template, accessing each one by the name you defined in the context:

{% extends 'curriculo/base.html' %}
{% block content %}

<form action="" method="post">
    {% csrf_token %}
    {{ form1 }}
    <input type="submit" class="btn btn-info" value="Salvar Idioma">
</form>
<form action="" method="post">
    {% csrf_token %}
    {{ form2 }}
    <input type="submit" class="btn btn-info" value="Salvar Lingua">
</form>

{% endblock content %}
    
20.12.2016 / 08:43