How do I register multiple records

0

I want to make several contact records, I wanted them to be more dynamic, for example, the person has a number of phones or cell phones or email's and while on the site, he can click on a sum sign and the page presents new boxes to enter this data and then, you would need to register all this information in the database. How can I do it?

insert.html

<div class='col-sm-12 padding-top-3'>
      <legend>Outros Contatos</legend>
        <table class='table table-hover table-bordered'>
          <tr>
            <th class='text-center'>Telefone</th>
            <th class='text-center'>Celular</th>
            <th class='text-center'>E-mail</th>
            <th class='text-center'></th>
          </tr>
          <tr>
            <td class='text-center'>{{ formCont.tel }}</th>
            <td class='text-center'>{{ formCont.cel }}</th>
            <td class='text-center'>{{ formCont.email }}</th>
            <th class='text-center'> + </th>
          <tr>
        <table>
    </div>

models.py:

class RegisterContact(models.Model):
tel = models.CharField('Telefone', max_length = 15, blank=True, null=True)
cel = models.CharField('Celular', max_length = 20, blank=True, null=True)
email = models.EmailField('Email', blank=True, null=True)
ref_pes = models.OneToOneField(Pessoa)

def __str__(self):
    return self.tel

class Meta:
    verbose_name = 'Contato'
    verbose_name_plural = 'Contatos'

views.py:

def insert(request):
form = PessoaForm(request.POST or None)
formCont = RegisterContactForm(request.POST or None)

if form.is_valid():
    resp = form.save()
    formCont.save(resp.id)
    return redirect('accounts:home')

template_name = 'accounts/insert.html'
context = {
    'form': form,
    'formCont': formCont,
}
return render(request, template_name, context)
    
asked by anonymous 31.12.2017 / 03:42

1 answer

0

What you want can be easily solved with AJAX.

Create a script and put a function like so:

function delete_comment(comment_id) {
    //coloque sua url que leva a view de inserir contactos
    var insert_contact_url = "/url/insert_contact/";
    var contact_data = new FormData();

    var phone = $('#phone_input').val();
    var cellphone = $('#cellphone_input').val();
    var email = $('#email_input').val();
    var person = $('#person').val()

    //cheque a documentação de como passar o CSRF Token no ajax
    contact_data.append('csrfmiddlewaretoken', csrftoken);
    //Aqui será adcionado os dados que será eviado no POST
    contact_data.append('phone', phone);
    contact_data.append('cellphone', cellphone);
    contact_data.append('email', email);
    contact_data.append('person', person);
    $.ajax({
      url: insert_contact_url,
      type: 'POST',
      data: contact_data,
      processData: false,
      contentType: false,
      success: function(data){
          alert('Contato adicionado com sucesso');
      }
    });
}

You are using django forms in case you can pass the information like this:

var contact_data = new FormData($('#form_id'));

This will save the data with their names in the object.

You can adpate the code the way you want, what matters and pass a FormData to AJAX.

Remembering that I used Jquery in this function.

    
31.12.2017 / 20:54