How do I register several records

Asked

Viewed 94 times

0

I want to do multiple contact records, I wanted it to be more dynamic, for example, the person has a number of phones or cell phones or emails and while on the site, she can click on a summation sign and the page presents new text boxes to enter this data and then, she would need to register all this information in the database. How can I do?

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>

py.models:

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'

py views.:

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)
  • Please arrange the indentation of your Python code. Paste the code here and use the button {} to format - you are just indenting the first line, and making your languages invalid code in the process.

  • Search By Formsetfactory. This will suit you if you haven’t solved the problem after so long.

1 answer

0

What you want can be easily solved with AJAX.

Create a script and put a function like this:

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 to the object.

You can adjust the code however you want, what matters and pass a Formdata to AJAX.

Remembering that I used Jquery in this function.

Browser other questions tagged

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