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