How to put value in Django field goes HTML?

0
Hello, I'm starting with Django now and wanted to know how to pass the value of the database to a Djan 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>
    
asked by anonymous 21.11.2018 / 08:09

1 answer

0

Okay, come on. You can do something like this: Here we will create a method to get the Bank data and pass a dictionary to the 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 in the way that 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 do not recommend that you do this. 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 to have answered, any questions ask there. = D

    
21.11.2018 / 10:59