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

Asked

Viewed 1,773 times

0

Guys, I have a problem in my application and I can’t find the solution on the internet.

I’m making a web application in which I need to get some user information and to improve the experience, I’m separating the form into several pages and I come across the problem of not being able to identify the registration id of the previous html information.

I’ve thought about taking this information by passing referencing id in the url, but I think it would not be a good solution due to security issues.

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

Follows parts of the code for use as 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 request after filling and sending the fomulário, the same makes the registration in the database and asks for the next 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>

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

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, create an independent temporary session cookie for each customer, and use this cookie to redeem the specific data of each session in progress and make available in the variable request.session, where the data you store in it is never sent to the client, it is only on the server.

Browser other questions tagged

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