Django数据库相关操作

  首先,在settings.py中加入应用的名称,让数据库知道使用的是哪个应用,然后给那个应用创建表。

  在settings.py中配置数据库相关参数,默认使用sqlite3不用配置

  编辑models.py文件,逻辑视图构建

  在pycharm中的终端通过数据库构建表结构,执行命令:

 

  在index.html中写入一个form表单,用于用户数据提交,还有一个用户信息返回table

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>{{ title }}</title>
 6 </head>
 7 <body>
 8     <h1>BookInfo</h1>
 9     <form action="/index/" method="post">
10         <input type="text" name="username">
11         <input type="password" name="password">
12         <input type="submit" name="up">
13     </form>
14     <!-- put the form to the views.py, and do action-->
15 
16     <hr/>
17     <h1>Show Info</h1>
18     <table>
19         <thead>
20             <th>name</th>
21             <th>password</th>
22         </thead>
23         <tbody>
24 
25             {% for line in user_list %}
26                 <tr>
27                     <td>{{ line.user }}</td>
28                     <td>{{ line.pwd }}</td>
29                 </tr>
30             {% endfor %}
31 
32         </tbody>
33     </table>
34 </body>
35 </html>

  在views.py中进行业务逻辑编写

   重启服务,刷新浏览器,用户交互的数据可以保存到数据库中。

  一个要素齐全,通过框架就可以将需要的展示出来。

 

posted @ 2017-11-02 15:00  今夜无风  阅读(174)  评论(0编辑  收藏  举报