Python+HTML+Sqlalchemy error

Asked

Viewed 34 times

0

Hello I am trying to do a simple procedure, but I stopped at the problem below. I have the simple goal of passing a parameter and filtering a list of associates. I have the following codes:

Python

@app.route("/listafiltrada/<int:doc>", methods=['GET', 'POST']) def listafiltrada(doc): associados = Associado.query.filter_by(Documento=doc) return render_template("listafiltrada.html", associado=associados)

This is the code that sends the parameter:

<form action="{{ url_for('listafiltrada') }}" method="GET">
        Nome<input type="text" id="doc" name="doc">
        <input type="submit" value="FILTRAR">
        <br>
    </form>

And this is the code you get:

<body>
    <h1>Lista Filtrada</h1>

    <form action="" method="POST">
        <table border="4">
            <tr>
                <td>IdAssociado</td>
                <td>Nome</td>
                <td>Telefone</td>
                <td>Documento</td>
                <td>E-mail</td>
                <td>Excluir</td>
                <td>Atualizar</td>
            </tr>
            {% for i in associado %}
            <tr>
                <td>{{ i.IdAssociado }}</td>
                <td>{{ i.Nome }}</td>
                <td>{{ i.Telefone }}</td>
                <td>{{ i.Documento  }}</td>
                <td>{{ i.Email }}</td>
                <td><a href="/excluir/{{ i.IdAssociado }}">X</a></td>
                <td><a href="/atualizar/{{ i.IdAssociado }}">Atualizar</a></td>
            </tr>
            {% endfor %}
        </table>
    </form>

    <a href="/index">Voltar</a>
</body>

But when executing the procedure the following error appears: werkzeug.routing.Builderror: Could not build url for 'filtered listed' endpoint. Did you Forget to specify values ['doc']?

How do we fix this? Thank you

1 answer

0

Setting parameters like you created in the view requires the parameter ("doc") to be passed in the path part of the URL, not as given from a GET.

That is, this configuration is expecting something like:

http://localhost/23, but GET for the form is passing the parameter as: http://localhost/?doc=23

Change your view code to something like:

@app.route("/listafiltrada/", methods=['GET'])
def listafiltrada():
    doc = request.args.get("doc")
    if not doc:
         # redireciona para view de quando não houver o ID

    associados = Associado.query.filter_by(Documento=doc)
    return render_template("listafiltrada.html", associado=associados)
  • Hello jsbueno, thank you for your reply. It worked well, only had to change the code "doc = request.get("doc")" to "doc = request.args.get('doc')", because the system had returned me the following error: "Attributeerror: 'Request' Object has no attribute 'get'". Thank you very much!

  • This - I tidied up in the reply - I thought one thing and typed another - is that the get is used in dictionaries, and .GET is used in the request object of some other frameworks (instead of "args")

Browser other questions tagged

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