flask - Display database from python to html

后端 未结 3 1404
既然无缘
既然无缘 2021-02-01 09:40

I have code like this to retrieve data from database and I want to display it in html.

This is app.py

@app.route(\'/news\')
def news():
         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-01 09:51

    render_template allows you to pass variables to html, and jinja2 help you to manipulate it. You only need to format your query result and send it within render_template

    Example

    app.py

    @app.route('/test')
    def test_route():
        user_details = {
            'name': 'John',
            'email': 'john@doe.com'
        }
    
        return render_template('test.html', user=user_details)
    

    test.html

    
    
        
            test
        
        
            
            

    {{user.name}}

    {{user.email}}

    to make the most of jinja2, take a look at his Documentation

提交回复
热议问题