• django查看数据库


    #views
    import pymysql
    def get_date(request):
        conn = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            passwd='mysql',
            db='homework2',
            charset='utf8',
            autocommit=True
        )
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        # sql = "select * from ?"
        # sql = sql.replace('%s','?')
        sql = "select * from teacher_info"
        # value = "teacher_info"
        # cursor.execute(sql,value)
        cursor.execute(sql)
        user_list = cursor.fetchall()
    
        with open(r'templates/get_data.html','r',encoding='utf8') as fr:
            data = fr.read()
        return render(request,'get_data.html',{'user_list':user_list})
    
    #html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>get_data</title>
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
        <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://use.fontawesome.com/aaee4b5d24.js"></script>
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <h2 class="text-center">数据展示</h2>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>id</th>
                            <th>name</th>
                            <th>age</th>
                            <th>salary</th>
                            <th>job</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in user_list %}
                            <tr>
                                <td>{{ user.id }}</td>
                                <td>{{ user.name }}</td>
                                <td>{{ user.age }}</td>
                                <td>{{ user.salary }}</td>
                                <td>{{ user.job }}</td>
                            </tr>
    
                        {% endfor %}
    
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    </body>
    </html>
    
  • 相关阅读:
    面试问烂的 MySQL 四种隔离级别,看完吊打面试官!
    注解Annotation实现原理与自定义注解例子
    趣图:苦逼的后端工程师
    session深入探讨
    趣图:听说996工作可以获得巨大成长
    面试官:一个 TCP 连接可以发多少个 HTTP 请求?
    聊聊前后端分离接口规范
    趣图:什么?需求文档又改了
    ASP.NET页面中去除VIEWSTATE视
    C#
  • 原文地址:https://www.cnblogs.com/agsol/p/11908419.html
Copyright © 2020-2023  润新知