Formulário Django

Asked

Viewed 84 times

-1

Good morning Guys, I’m doing a Jango project and I already have a template that I worked on, I want to use the forms of this template to make your operations with Django. What I’ve been able to do so far is replicate the forms as the Django forms themselves, but I want to use the existing forms. My code currently looks like this: HTML

<div class="modal-body">
                    <div class="form-group form-modal col-12">
                        <div class="row">
                            <label for="seguimento-cad" class="col-form-label col-3">Seguimento: </label>
                            <select class="form-control form-control-sm col-9" id="seguimento-cad" >
                                <option>Masculino</option>
                                <option>Feminino</option>
                            </select>
                        </div>
                        <div class="row">
                            <label for="cidade-cad" class="col-form-label col-3">Cidade: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="cidade-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="bairro-cad" class="col-form-label col-3">Bairro: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="bairro-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="endereco-cad" class="col-form-label col-3">Endereço: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="endereco-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="numero-cad" class="col-form-label col-3">Número: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="numero-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="complemento-cad" class="col-form-label col-3">Complemento: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="complemento-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="cep-cad" class="col-form-label col-3">Cep: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="cep-cad" style="width: 320px">
                        </div>
                        <div class="row">
                            <label for="telefone-cad" class="col-form-label col-3">Telefone: </label>
                            <input type="text" class="form-control form-control-sm col-9" id="telefone-cad" style="width: 320px">
                        </div>
                    </div>
                </div>

Forms.py

class empresa_cad(ModelForm):
class Meta:
    model = Empresa
    fields = ['nomeempresa', 'seguimentoempresa', 'cnpjempresa', 'seguimentoempresa', 'emailempresa',
              'senhaempresa', 'cepempresa', 'ufempresa', 'cidadeempresa', 'bairroempresa', 'logradouroempresa',
              'numeroenderecoempresa', 'complementoempresa']

View

def empresas(request):
empresas_list = Empresa.objects.all()
paginator = Paginator(empresas_list, 10)
page = request.GET.get('page')
empresas = paginator.get_page(page)

form_new = empresa_cad(request.POST or None, request.FILES or None)
if form_new.is_valid():
    form_new.save()

return render(request, 'core/empresas.html', {'empresas': empresas, 'form_empresa': form_new,
                                              'ultimo_id': ultimo_id)

When setting {{ form_new }} inside the form the register works but I do not know how to make the fields of Django be used by the existing form.

1 answer

0

To use this form, you will have to use the same Names and id’s, for this, you will have to set the id’s and Names of the Forms, you can do so:

<div class="row">
    <label for="cidade-cad" class="col-form-label col-3">Cidade: </label>
    <input type="text" class="form-control form-control-sm col-9" name="{{ form.cidadeempresa.name }}" id="{{ form.cidadeempresa.auto_id }}" style="width: 320px">
</div>
<div class="row">
    <label for="bairro-cad" class="col-form-label col-3">Bairro: </label>
    <input type="text" class="form-control form-control-sm col-9" name="{{ form.bairroempresa.name }}" id="{{ form.bairroempresa.auto_id }}" style="width: 320px">
</div>

Note the "{{ form.citeempresa.auto_id }}" and {{ form.citeempresa.name }}, this will generate the id’s and Names of the fields, which will be necessary for you to receive the values via post.

  • 1

    Thank you very much Victor, I managed to apply in my code :D

  • I’m glad I could help ^^

Browser other questions tagged

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