How to open a screen with pre-filled form with HTML?

0

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>
    
asked by anonymous 11.04.2018 / 19:00

2 answers

0

In your view, when instantiating the form, add the instance of the desired object.

meu_objeto = MyObject.objects.get(pk=meu_id)
form = MyForm(instance=meu_objeto)

This should solve your problem.

    
12.04.2018 / 00:32
0

To manually fill in the value of the input tags in your form you need to reference the fields in the Django form, get .value of them, and fill in value input , as in the example below:

<input type="text" name="nome" id="nome" value="{{ form.nome.value|default:"" }}" required autofocus>
{# Tome nota para a tag "default" que é útil quando o campo está vazio pois em alguns casos ele ficaria mostrando o campo nulo "None" do Python #}
    
29.04.2018 / 21:35