UPDATE ON DJANGO

Asked

Viewed 164 times

0

I’m a beginner in Django and I’m trying to crud, create, read and delete are working, but the update is not working. Please help me out. This is the page with the data listing:

<table class="table table-striped table-dark">
            <tr>
                <th>ID</th>
                <th>NOME</th>
                <th>CPF</th>
                <th>EMAIL</th>
                <th>SENHA</th>
            </tr>
        {% for dado in dados %}
            <tr>
                <td>{{ dado.id }}</td>
                <td>{{ dado.nome }}</td>
                <td>{{ dado.cpf }}</td>
                <td>{{ dado.email }}</td>
                <td>{{ dado.senha }}</td>
                <td><a href="{% url 'atualiza' dado.id %}"><button class="btn-primary">EDITAR</button></a></td>
                <td><a href="{% url 'delete' dado.id %}"><button class="btn-primary">EXCLUIR</button></a></td>
            </tr>
        {% endfor %}
        </table>

The url that redirects to the page with the form:

path('atualiza/<int:id>', views.atualiza, name='atualiza'),

The form:

<form action="." method="POST">
        {% csrf_token %}
        <h3>Insira os dados para alteraçao</h3>
        <input type="text" name="id" value="{{ id.id }}" placeholder="ID" disabled>
        <input type="text" name="nome" id="nome" value="{{ id.nome }}" placeholder="Nome">
        <input type="password" name="senha" id="senha" value="{{ id.senha }}" placeholder="Senha">
        <input type="text" name="email" id="email" value="{{ id.email }}" placeholder="Email">
        <button type="submit">ENVIAR</button>
    </form>

The view:

def atualiza(request, id):
    id = Login.objetos.get(id=id)
    update = Cadastro(request.POST, instance=id)
    if update.is_valid():
        nome = update.cleaned_data['nome']
        senha = update.cleaned_data['senha']
        email = update.cleaned_data['email']
        query = Login(nome=nome, senha=senha, email=email)
        query.save()
        return render(request, 'index.html')
    else:
        return render(request, 'update.html', {'id': id}, {'update': update})

When I click edit, it redirects to the page with the completed form, but when I click send the error PAGE NOT FOUND. Thank you for your cooperation.

1 answer

0

In your form the action="." may be changing the url where the post is being made. Just try <form method="post">

  • Thanks for your help, but I tried to do this but I did not succeed, when I leave without the action and click the send button, nothing happens, I simply remain on the page that the form is open. Searching a little, I saw a different way to do the action, I put it like this: action="{% 'update' id.id %}", but when I click to send no action happens

Browser other questions tagged

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