时间:2021-07-01 10:21:17 帮助过:4人阅读
nid = models.AutoField(primary_key=True) #自增id(可以不写,默认会有自增id) title = models.CharField(max_length=32) publishDdata = models.DateField() #出版日期 author = models.CharField(max_length=32) price = models.DecimalField(max_digits=5,decimal_places=2) #一共5位,保留两位小数
执行命令创建:(需要记住的!!!)
python3 manage.py makemigrations 创建脚本 python3 manage.py migrate 迁移
具体例子实现
model.py
urls.py
views.py
template /index.html(用来渲染数据的模版)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width"> <title>Title</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> <style> table{ margin-top: 50px; } </style> </head> <body> <div class="containers"> <div class="row"> <div class="col-md-9 col-md-offset-2"> <table class="table table-hover"> <thead> <tr> <th>编号</th> <th>书名</th> <th>出版日期</th> <th>作者</th> <th>价钱</th> <th>操作</th> </tr> </thead> <tbody> {% for book in book_list %} <tr> <td>{{ book.nid }}</td> <td>{{ book.title }}</td> <td>{{ book.publishDdata|date:‘Y-m-d‘ }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> <td> <a href="/del/{{ book.nid }}"><button class="btn btn-danger">删除</button></a> <a href="/edit/{{ book.nid }}"><button class="btn btn-success">编辑</button></a> <a href="/add/"><button class="btn btn-primary">添加</button></a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
五、查看数据库的sql语句(加在settings.py)
查看数据库执行代码 LOGGING = { ‘version‘: 1, ‘disable_existing_loggers‘: False, ‘handlers‘: { ‘console‘:{ ‘level‘:‘DEBUG‘, ‘class‘:‘logging.StreamHandler‘, }, }, ‘loggers‘: { ‘django.db.backends‘: { ‘handlers‘: [‘console‘], ‘propagate‘: True, ‘level‘:‘DEBUG‘, }, } }
Django 【第四篇】ORM数据库基础
标签:migrate col 介绍 href init src 命令 lis rip