How to Put Value in the Django Will HTML Field?

Asked

Viewed 351 times

0

Hello, I’m starting with Django now and wanted to know how to pass the value of the database to a Djando field using HTML to save the edit.

Na minha View:

@login_required
def editar_aluno(request, id):
    aluno = Aluno.objects.get(id=id)
    if request.POST:
        form = FormAluno(request.POST, instance=aluno)
        if form.is_valid():
            form.save()
            return redirect(lista_aluno)
    else:
        form = FormAluno(instance=aluno)
    return render(request, 'editar_aluno.html', locals())
<form role="form" class="form-horizontal" method="post" action="#" enctype="multipart/form-data">
                          {% csrf_token %}
                        <div class="form-group">
                          <label class="col-md-2 control-label">Nome</label>
                          <div class="col-md-10">
                            <input type="text" required placeholder="Nome" id="nome" class="form-control" name="nome" style="width:60%">
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-md-2 control-label">Pai*</label>
                          <div class="col-md-10">
                            <input type="text" placeholder="Pai" id="pai" class="form-control" name="pai" style="width:60%">
                          </div>
                        </div>

1 answer

1

All right, come on.
You can do something like this: Here we create a method to take the database data and pass a dictionary to html:

def arquivos(request):
    arquivos = arquivos.objects.filter(published_date__lte=timezone.now()).order_by('data') 
    return render(request, 'template/exibir_arquivos.html', {'arquivos': arquivos})

In html we use this syntax of {%%} to write some code and create a logic in html, so we can display the data the way it is being shown in the example:

{% for arquivo in arquivos %}
    <div>
        <p>publicado em: {{ arquivo.data }}</p>
        <h1><a href="">{{ arquivo.titulo }}</a></h1>
        <p>{{ arquivo.descricao }}</p>
    </div>
{% endfor %}

However... I don’t recommend doing that. In my case, I create several routes with an API in Django and display the data with Javascript, so I can separate the Front from the Back. And in case of an integration the API is already ready. I hope I have answered any question ai. = D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.