1
Hello, I am doing a job that needs to insert values in the database, return them in a table and be able to delete them through Flask. I am knowing the tool and I could not find solutions. Be able to insert in one page and view and delete in another, all working correctly, but the problem comes when together in a single page, where it works only insertion.
This is the HTML part:
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">LINK / IP BLOQUEADO</th>
<th scope="col">REMOVER</th>
</tr>
</thead>
<tbody>
{% for row2 in listaIpLinks %}
<tr>
<th scope="row">{{row2.0}}</th>
<td>{{row2.1}}</td>
<td>
<form method="post" action="{{ url_for('deleteIpLink', id=row2.0) }}">
<input type="submit" value="Delete"></i>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{%from "_formhelpers.html" import render_field %}
<form method="post" action="{{ url_for('ipLink') }}">
<dl>
{{render_field(form.ipLink)}}
</dl>
<p><input type="submit" value=Register></p>
</form>
Here’s the party responsible for Flask:
@app.route('/getIpLink/', methods=["GET","POST"])
def ipLink():
form = ipLinkForm(request.form)
try:
if form.validate():
ipLink = form.ipLink.data
mycursor,mydb = connection()
mycursor.execute('INSERT INTO linksespecificos(ALVO) VALUES ("'+ipLink+'")')
mydb.commit()
flash("LINK/IP inserido com sucesso")
mycursor.close()
mydb.close()
gc.collect()
return redirect(url_for("getIpLink"))
return render_template("getIpLink.html", form=form)
except Exception as e:
return(str(e))
@app.route('/getIpLink/')
def getIpLink():
try:
mycursor,mydb = connection()
mycursor.execute('SELECT * FROM linksespecificos')
data = mycursor.fetchall()
mycursor.close()
return render_template("getIpLink.html", listaIpLinks=data)
except Exception as e:
return(str(e))
@app.route('/getIpLink/<id>', methods=["GET","POST"])
def deleteIpLink(id):
try:
mycursor,mydb = connection()
mycursor.execute('DELETE FROM linksespecificos WHERE ID="'+id+'"')
mycursor.close()
flash("Link deletado!")
return redirect(url_for('getIpLink'))
except Exception as e:
return(str(e))
I apologize, but I’m starting now in the area and I don’t have much knowledge, I’m proud to have gotten this far, but I stuck to that part. If anyone can help, I would be enormously grateful!