Get input by view

1

I have a form with inputs: email, first and last name. The email will always have a defined format, so I want to get the elements and have them automatically fill in the other fields, for example:

User types email Email: [email protected] (emails will always have this format), so I want the form to fill in the other fields: First Name: carina Last name: joao

views.py

def cadastro(request):
    if request.method == 'POST':
        return create(request)
    else:
        return new(request)


def new(request):
    return render(request, 'cadastro1.html',
                {'form': CadastroForm()})


def create(request):
    form = CadastroForm(request.POST)
    ponto = form.email.index(".")
    arroba = form.email.index("@")
    first = form.email[0:ponto]
    second = form.email[ponto + 1:arroba]

    obj = form.save()
    if not form.is_valid():
      return render(request, 'cadastro1.html',
                    {'form': form})

    return HttpResponseRedirect('/cadastro/%d/' % obj.pk)

I already have the code to go through, but I am not aware of playing in the fields

    
asked by anonymous 14.08.2017 / 13:57

1 answer

1

You can capture the email field and break the string :

getEmail = request.POST['email'] #Pega o email
email = getEmail.split('@') #Quebra a string antes dps do @
nomes = email[0].split('.') #Quebra a string antes e dps do .
nome = nomes[0] #Aqui vc tem o primeiro nome
sobrenome = nomes[1] #Aqui vc tem o sobrenome

Now you save:

user = User()
user.email = getEmail
user.nome = nome
user.sobrenome = sobrenome
user.save()

But as they said, the best option is to do front with js and automatically populate the form and send it back / p>     

11.01.2018 / 18:50