1、创建model类

app01/models.py
1
2
3
4
5
6
7
from django.db import models
 
# Create your models here.
class UserInfo(models.Model):  #创建model类,必须继承自models.Model类
    # 设计表结构和字段,最大长度,默认表名app01_userinfo
    email = models.CharField(max_length=16)
    pwd = models.CharField(max_length=20)

2、注册app

settings.py
1
2
3
4
5
6
7
8
9
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
]

3、执行命令创建表

cmd> python manage.py makemigrations       生成源信息,一个字典,位置:app01/migrations/0001_initial.py

cmd> python manage.py migrate     读取生成的源信息,去生成数据库表

4、查看

4.1、用navicat-SQLite工具查看

4.2、用程序查看
app01/admin.py
1
2
from app01 import models
admin.site.register(models.UserInfo)     #把UserInfo创建的表注册进admin页面

浏览器访问http://127.0.0.1:8000/admin/

点进去后就可以添加用户

5、现在有一个用户user1,登录成功后跳转到index页面,列出数据库表里的所有用户信息。

app01/views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render
from django.shortcuts import redirect
def login(request):
    if request.method == 'POST':
        input_email = request.POST['email']
        input_pwd = request.POST['pwd']
        if input_email == 'user1@qq.com' and input_pwd == '123':
            return redirect("/index/")      #登录成功后跳转到index页面
        else:
            return render(request,"login.html",{"status":"用户名密码错误"})
    return render(request,'login.html')
 
def index(request):
    #从数据库中获取数据,并和html渲染
    from app01 import models
    #获取表中所有数据
    user_info_list = models.UserInfo.objects.all()
    return render(request,'index.html',{"user_info_list":user_info_list,})
urls.py
1
2
3
4
5
6
7
8
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login),
    url(r'^index/', views.index),
]
templates/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div>
        <table>
            <thead>
                <tr>
                    <th>邮箱</th>
                    <th>密码</th>
                </tr>
            </thead>
            <tbody>
                {% for line in user_info_list %}
                    <tr>
                        <td>{{ line.email }}</td>
                        <td>{{ line.pwd }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</body>
</html>

浏览器访问 http://127.0.0.1:8000/login/

登录成功后跳转到 http://127.0.0.1:8000/index/,列出所有的刚才手动在页面创建的用户。

6、model添加数据

index.html
1
2
3
4
5
<form action="/index/" method="post">
    <input type="text" name="em" />
    <input type="text" name="pw" />
    <input type="submit" value="添加" />
</form>
views.py
1
2
3
4
5
6
7
8
9
10
11
def index(request):
    #从数据库中获取数据,并和html渲染
    from app01 import models
    #添加数据
    if request.method=='POST':
        input_em = request.POST['em']
        input_pw = request.POST['pw']
        models.UserInfo.objects.create(email=input_em,pwd=input_pw)
    #获取表中所有数据
    user_info_list = models.UserInfo.objects.all()
    return render(request,'index.html',{"user_info_list":user_info_list,})

7、model删除数据

index.html
1
2
3
4
5
<form action="/index/" method="post">
    <input type="text" name="email_del">
    <input type="text" name="pwd_del">
    <input type="submit" value="删除">
</form>
views.py​
1
2
3
4
5
6
def index(request):
    from app01 import models
    #删除数据
    if request.method=='POST':
        input_email = request.POST['email_del']
        models.UserInfo.objects.filter(email=input_email).delete()

8、model更新数据

index.html
1
2
3
4
5
<form action="/index/" method="post">
    <input type="text" name="email_update">
    <input type="text" name="pwd_update">
    <input type="submit" value="更新">
</form>
views.py
1
2
3
4
5
6
def index(request):
    #更新数据
    if request.method=='POST':
        input_em = request.POST['email_update']
        input_pw = request.POST['pwd_update']
        models.UserInfo.objects.filter(email=input_em).update(pwd=input_pw)

9、页面验证:

最新文章

  1. html 学习(一)
  2. [原创]cocos2d-x研习录-第三阶 背景音乐和音效
  3. 使用WITH AS提高性能简化嵌套SQL
  4. 慕课网-安卓工程师初养成-3-9 Java中运算符的优先级
  5. C# - 创建List属性的简单方法
  6. 水晶报表在vs2010&nbsp;WPF环境下的尝试
  7. 编写简单的辅助脚本来在 Google 表格上记账
  8. requests-证书验证
  9. 在golang中使用leveldb
  10. Android Third Party Libraries and SDK&#39;s
  11. Windows服务器【由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作】问题调查
  12. Effective Java 第三版——81. 优先使用并发实用程序替代wait和notify
  13. 文件操作(十二)——open,read,close,write,seek,truncate
  14. mysql 增加只读用户查询指定表
  15. Java如何在指定端口创建套接字?
  16. 远程获得乐趣的 Linux 命令
  17. js 操作select和option常用代码整理
  18. Ubuntu安装Python的mysqlclient
  19. Delphi获取毫秒级时间戳
  20. web.xml配置文件详解

热门文章

  1. 转载关于reset vector 和 exception vector
  2. FastAdmin CMS 插件相关文章收集(2018-08-16)
  3. (转)Android 自定义 spinner (背景、字体颜色)
  4. super,this
  5. 深入浅出K-Means算法
  6. 机器人操作系统(ROS)教程22:ROS的3D可视化工具—rviz
  7. Java中判断非空对象.
  8. python开发mysql:视图、触发器、事务、存储过程、函数
  9. C# winform中PictureBox控件的SizeMode模式
  10. Linux性能监测:磁盘IO篇