How to pass data from one input to another with HTML and Python?

0

Well, I have a problem with my application and I can not find the solution on the internet.

I'm doing a web application where I need to get some information from the user and to improve the experience, I'm separating the form into several pages and I'm having trouble identifying the registration id of the previous html information.

I thought of getting this information by passing reference id in the url, but I do not think it would be a good solution due to security issues.

In this case, would it be best to pass the encrypted id in the url or would you have some way to pass this id to the next page?

Follow parts of the code for reference:

First part of the form:

<form method="POST" action="segurado/">
  {% csrf_token %}
  Nome completo:<br>
  <input type="text" name="nome" maxlength="100" required=""><br> Sexo:

  <br>
  <input type="radio" name="genero" value="masculino" checked> Masculino
  <input type="radio" name="genero" value="feminino"> Feminino<br> e-mail:

  <br>
  <input type="email" name="e-mail" maxlength="50" required=""><br> Telefone:

  <br>
  <input type="tel" name="telefone" maxlength="30" required=""><br>

  <input type="submit">

</form>

View that the input makes a request after completing and sending the form, it makes the registration in the database and asks for the following information:

if request.method == 'POST':
    # verificando se existe algum telefone ja cadastrado
    try:
        telefone['numero'] = str(request.POST.get('telefone'))
        Telefone.objects.get(numero=telefone['numero'])
    except Exception as e:
        mensagem['mensagem_telefone'] = str(e)

        # cadastrando o telefone caso seja a primeira vez
        try:
            Telefone.objects.create(**telefone)
        except Exception as e:
            mensagem['mensagem_telefone2'] = str(e)
        else:
            mensagem['mensagem_telefone2'] = "Cadastro Realizado"
    else:
        mensagem['mensagem_telefone'] = "Ja cadastrado"

    # verificando se tem algum email cadastrado
    try:
        email['email'] = request.POST.get('e-mail')
        Email.objects.get(email=email['email'])
    except Exception as e:
        mensagem['mensagem_email'] = str(e)

        # cadastrando caso seja o primeiro e-mail
        try:
            Email.objects.create(**email)
        except Exception as e:
            mensagem['mensagem_email2'] = str(e)
        else:
            mensagem['mensagem_email2'] = "Cadastro Realizado"
    else:
        mensagem['mensagem_email'] = "Ja cadastrado"

    # realizando o cadastro do Usuario
    try:
        usuario['nome'] = request.POST.get('nome')
        usuario['genero'] = request.POST.get('genero')
        novo_usuario = Usuario.objects.create(**usuario)
    except Exception as e:
        mensagem['mensagem_usuario'] = e
    else:
        mensagem['mensagem_usuario'] = "cadastro realizado com sucesso"

        # realizando o cadastro do email
        try:
            email = request.POST.get('e-mail')
            cadastro = Email.objects.get(email=email)
            novo_usuario.emails.add(cadastro)
        except Exception as e:
            mensagem['mensagem_email3'] = str(e)
        else:
            mensagem['mensagem_email3'] = "Cadastro do email no cliente realizado com sucesso"

        # realizando o cadastro do telefone
        try:
            telefone = request.POST.get('telefone')
            cadastro = Telefone.objects.get(numero=telefone)
            novo_usuario.telefones.add(cadastro)
        except Exception as e:
            mensagem['mensagem_telefone3'] = str(e)
        else:
            mensagem['mensagem_telefone3'] = "Cadastro do telefone no cliente realizado com sucesso"

    return render(request, 'cotacao/segurado.html')

else:
    return redirect('cotacao_usuario')

Html page of the second part of the form:

<form method="POST" action="/cotacao/veiculo/">

  {% csrf_token %} CPF/CNPJ:

  <br>
  <input type="text" name="cpf/cnpj" required><br> Data de Nascimento:<br>
  <input type="date" name="nascimento" required><br>

  <input type="submit" name="enviar" value="Prosseguir">

</form>

Now I have the problem of knowing how I will have the Id of the first register to continue the registration of the other information in my database.

    
asked by anonymous 28.10.2018 / 21:18

1 answer

0

Use a session variable to store the id:

request.session['id_cadastro'] = algum_tipo_de_id

then on the other page you can read the id

id_atual = request.session.get('id_cadastro', None)
if not id_atual:
    # chegou sem ter passado pela primeira parte do form, redireciona
    return redirect('pagina1')
...

Django will manage everything for you, will create an independent temporary session cookie for each client, and use this cookie to retrieve the specific data of each session in progress and make it available in the request.session variable, and the data that you store on it are never sent to the client, they are only on the server.

    
29.10.2018 / 02:25