How I create 2 Foms simultaneously Django

Asked

Viewed 932 times

1

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 make an intermediate form? Am I making a point wrong?

... 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__'

.... py views.

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

.... html language.

{

% 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 %}

1 answer

0


The context passed to the render function is a dictionary, you can pass as many arguments as you want 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 %}
  • Thank you, I guess I didn’t quite understand the workings of a dictionary. Thank you again

Browser other questions tagged

You are not signed in. Login or sign up in order to post.