How can I edit data fetched from db in html table ? Flask, Jinja2

Question:

I’m afraid the title isn’t so clear as I want it to be, but I’ll try to explain more detailed:

I had a db’s table :

class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    task = db.Column(db.String(10),index=True)
    descr = db.Column(db.String(300),index=True,unique=True)
    docs = db.relationship('Doc', backref='Task', lazy='dynamic'

this query docs = Task.query.all()

and some html page with a loop:

<table>
{% for doc in docs %}
<tr><td>{{doc.id}}</td><td>{{doc.task}}</td><td>{{doc.descr}}</td><td> 
{% endfor %} 
</table>  

this all works well and looks like usual html table filled with data from my db (sqlite). Like this, but it’s just another table, no matter.
enter image description here
Sometimes the data have missing values like the last column in this pic, it’s normal because I want to fill that later. How can I implement such functionality ? I’ve tried many things.. The problem is that i don’t know how to get another info from currently edited row to make correct record to database.

For example, lets’s take the second row and add some data to last column -> submit. Now i must somehow get from this row ‘234’ or ‘sfdfs’ to make

edit_ task = Task.query.filter_by(task=234).first() or 
Task.query.filter_by(descr=sfdfs).first() 

get the result of this query and simply add new info to unfilled column and than commit.

I can make different forms, requests and other things, but I just don’t understand how to make this function in propper way? Shoul i use JS to get info from table in the template, than transfer it to form and than to python /route function? or there is another decision ? may be this html table must be some kind of WTFotms table.widget form? Any ideas would be greatly appreciated. Thx

Asked By: V.Nosov

||

Answers:

http://www.blog.pythonlibrary.org/2017/12/14/flask-101-adding-editing-and-displaying-data/ the only way i managed to find. Just don’t create an html-table mannualy with cycling , Table Class from flask_table can make it for you and there is also an oppurtunity to make edit link.

Answered By: V.Nosov

Just found this blog post about Grid.js. It makes it possible to edit table cells directly on a page without extra Edit links.

Answered By: wikiselev
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.