1
I have a form with the inputs: email, name and surname. The email will always have a defined format, so I want to take the elements and make it fill automatically in the other fields, example:
User type in email Email: [email protected] (emails will always have this format), so I want the form to fill in the other fields: Name: Field Last name: John Paul
py views.
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, but I can’t play in the fields
You want the information to appear in the form while the user type or just type the email and you save directly in the bank, without the need for the fields (name and surname) to appear dynamically for the user?
– Sidon
Dynamically appearing
– Bianca C.
Then you will have to use javascript, see that you should not do this on the server side (the view ) but on the client side (in the template rendering). Create a script and load it along with the template then manipulate the event
onchange
of the email input element to fill in the fields you want. If you want a complete example, clone of that repository on github, to reproduce it step by step with instructions in English, see that answer.– Sidon
Okay, I’ll do that, thank you
– Bianca C.