I would like to know how to open a form whose fields are already filled in with user data (resulting from a search in the database), and it is up to the user to only edit the information.
I am using Django
to program, I was able to do the form, but I can not recover it for editing.
I even get you using the {{ form.as_p }}
function, however I would like to recover this data using <input>
, how do I?
Currently my view for editing is as follows:
def editar(request, pk): # Funcao para editar um aluno
post = get_object_or_404(Aluno, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('aluno:detalhes', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'aluno/editar.html', {'form': form})
And in html it looks like this:
<form method="POST" class="post-form">{% csrf_token %}
<!--Entrada do nome do aluno-->
<label class="campos" for="nome">Nome:</label><br>
<input type="text" name="nome" id="nome" required autofocus >
<p></p>