Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

python - Flask: How to show data clearly from database

I use Flask and Firebase for database.

I want to show 7 variables from database and i made it but not the way i wanted it.

When i get data from database , my screen looks like this :

enter image description here

I am trying to shown like this: enter image description here Without ' { } ' Heres my code: This is routes.py

@app.route('/satis', methods=['GET', 'POST'])
def satis():
    if (request.method == 'POST'):
        sehir=request.form['il']
        yas=request.form['yas']
        id=request.form['id']
        kat=request.form['kategori']
        gun=request.form['satisgunu']
        cins=request.form['cinsiyet']
        tarz=request.form['satistarzi']
        db = firebase.database()
        db.child("names").push({"sehir": sehir,"yas":yas,"id":id,"kat":kat,"gun":gun,"cins":cins,"tarz":tarz})
        todo = db.child("names").get()
        to = todo.val()
        return render_template('satis.html', t=to.values())
    return render_template('satis.html')

This is satis.html

<div class="container-fluid">
        {% for l in t %}
            <ul class="list-group">
                <li class="list-group-item">
                    <h4>{{l}}</h4>
                </li>
            </ul>
        {% endfor %}
    </div>
{% endblock %}
question from:https://stackoverflow.com/questions/65646657/flask-how-to-show-data-clearly-from-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can convert every row to string in code using for-loop, f-string and join()

data = [
    {'cins': 'kadin', 'gun': 'carsambra', 'id': '1', 'kat': 'cantra', 'sehir': 'izmir', 'tarz': '7', 'yas': '22'},
    {'cins': 'kadin', 'gun': 'pazartesi', 'id': '1', 'kat': 'cantra', 'sehir': 'ankara', 'tarz': '5', 'yas': '18'},
]

new_data = []

for row in data:
    text = ', '.join(f'{key}:{val}' for key, val in row.items())
    new_data.append(text)
    print(text)

Result

cins:kadin, gun:carsambra, id:1, kat:cantra, sehir:izmir, tarz:7, yas:22
cins:kadin, gun:pazartesi, id:1, kat:cantra, sehir:ankara, tarz:5, yas:18

and now you can send new data to template.


Or you may try to do the same in template using also for-loop, row.items() with key,val and also loo.last to skip , after last item

from flask import Flask, render_template_string

app = Flask(__name__)

@app.route('/')
def inder():

    data = [
        {'cins': 'kadin', 'gun': 'carsambra', 'id': '1', 'kat': 'cantra', 'sehir': 'izmir', 'tarz': '7', 'yas': '22'},
        {'cins': 'kadin', 'gun': 'pazartesi', 'id': '1', 'kat': 'cantra', 'sehir': 'ankara', 'tarz': '5', 'yas': '18'},
    ]

    return render_template_string("""
    {% for row in data %}
        <ul class="list-group">
            <li class="list-group-item">
                <h4>{% for key, val in row.items() %}{{key}}:{{val}}{{ ", " if not loop.last }}{% endfor %}</h4>
            </li>
        </ul>
    {% endfor %}
    """, data=data)

app.run(debug=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...