环境:pycharm,win10,mysql5.7,bootstrap-3.3.7
1.先在Terminal中创建django项目:
1: django-admin.py startproject book_manage
然后将目录切换到book_manage下,输入
1: python manage.py startapp app01
然后在book_manage中创建templates模版包,右键它Mark Directory as –> Templates Folder
目前的项目结构为
1: book_manage-{
2: app01
3: book_manage
4: templates
5: manage.py
6: }
2.在models.py中写入
1: class Book(models.Model):
2: id = models.AutoField(primary_key=True)
3: title = models.CharField(max_length=32, unique=True)
4: pub_date = models.DateField()
5: price = models.DecimalField(max_digits=8, decimal_places=2) # max number = 999999.99
6: publish = models.CharField(max_length=32)
7:
准备工作:
在settings中替换原来的sql
1: DATABASES = {
2: 'default': {
3: 'ENGINE': 'django.db.backends.mysql',
4: 'NAME': 'bms', # 要连接的数据库名,连接前需要创建好
5: 'USER': 'root', # 连接数据库的用户名
6: 'PASSWORD': '', # 连接数据库的密码
7: 'HOST': '127.0.0.1', # 连接主机,默认本机
8: 'PORT': 3306 # 端口 默认3306
9: }
10: }
确保配置文件中的INSTALLED_APPS中写入我们创建的app名称
1: INSTALLED_APPS = [
2: 'django.contrib.admin',
3: 'django.contrib.auth',
4: 'django.contrib.contenttypes',
5: 'django.contrib.sessions',
6: 'django.contrib.messages',
7: 'django.contrib.staticfiles',
8: "app01",
9: ]
然后我们需要找到book_manage.py包下的__init__.py文件,写入
1: import pymysql
2: pymysql.install_as_MySQLdb(
如果报错如下,参考单表操作的随笔:
1: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11.None
再在终端中使用两条数据库迁移命令
1: >>> python manage.py makemigrations
2: >>> python manage.py migrate
settings中TEMPLATES的DIRS改成
1: 'DIRS': [os.path.join(BASE_DIR, 'templates')],
这样mysql中的表就建好了,可以自行去查看
3.book_manage-urls.py写入如下
1: from django.contrib import admin
2: from django.urls import path, re_path
3: from app01 import views
4: urlpatterns = [
5: path('admin/', admin.site.urls),
6: path('add_book/', views.add_book),
7: ]
app01-views.py中写入视图函数
1: from app01.models import Book
2: def add_book(request):
3: if request.method == "POST":
4:
5: title = request.POST.get("title")
6: price = request.POST.get("price")
7: pub_date = request.POST.get("pub_date")
8: publish = request.POST.get("publish")
9:
10: book_obj = Book(title=title, price=price, pub_date=pub_date, publish=publish)
11: book_obj.save()
12: return redirect('/find_book/') # 这里是跳转到查找页面,可以改成Httprespones("OK!")
13:
14: return render(request, 'add_book.html')
下载bootstrap,在app01中创建一个static包,将bootstrap的dist包放入
1: app01-
2: static-
3: dist
在templates创建add_book.html文件,写入
1: <!DOCTYPE html>
2: <html lang="en">
3: <head>
4: <meta charset="UTF-8">
5: <title>Title</title>
6:
7: <link rel="stylesheet" href="/static/dist/css/bootstrap.css">
8: <style>
9: .container{
10: margin-top: 100px;
11: }
12: .btn{
13: margin-top: 10px;
14: }
15: h3{
16: margin-left: 40%;
17: }
18: </style>
19: </head>
20: <body>
21: <h3>添加书籍</h3>
22:
23: <div class="container">
24: <div class="row">
25: <div class="col-md-6 col-md-offset-3">
26: <form action="" method="post">
27: {% csrf_token %}
28: <div>
29: <label for="">书籍名称</label>
30: <input type="text" class="form-control" name="title">
31: </div>
32: <div>
33: <label for="">价格</label>
34: <input type="text" class="form-control" name="price">
35: </div>
36: <div>
37: <label for="">出版日期</label>
38: <input type="date" class="form-control" name="pub_date">
39: </div>
40: <div>
41: <label for="">出版社</label >
42: <input type="text" class="form-control" name="publish">
43: </div>
44: <input type="submit" class="btn btn-success pull-right ">
45: </form>
46: </div>
47: </div>
48: </div>
49: </body>
50: </html>
这样配置参数runserver 8000(也可以通过终端命令来运行Django),运行manage.py,浏览器http://127.0.0.1:8000/add_book/ 就能看到添加页面了。
4.其他的查看,删除,编辑功能代码如下:
views.py视图函数
1: def find_book(request):
2: book_list = Book.objects.all()
3:
4: return render(request, 'find_book.html', {'book_list': book_list})
5:
6:
7: def delete_book(request, id):
8: Book.objects.filter(id=id).delete()
9:
10: return redirect("/find_book/")
11:
12:
13: def update_book(request, id):
14: book_obj = Book.objects.filter(id=id).first()
15:
16: if request.method == 'POST':
17: title = request.POST.get("title")
18: price = request.POST.get("price")
19: pub_date = request.POST.get("pub_date")
20: publish = request.POST.get("publish")
21:
22: Book.objects.filter(id=id).update(title=title, price=price, pub_date=pub_date, publish=publish)
23: return redirect('/find_book/')
24:
25: return render(request, 'update_book.html', {"book_obj": book_obj})
find_book.html
1: <!DOCTYPE html>
2: <html lang="en">
3: <head>
4: <meta charset="UTF-8">
5: <title>Title</title>
6:
7: <link rel="stylesheet" href="/static/dist/css/bootstrap.css">
8: <style>
9: .container{
10: margin-top: 100px;
11: }
12: h3{
13: margin-left: 45%;
14: }
15: </style>
16: </head>
17: <body>
18:
19: <h3>查看书籍</h3>
20:
21: <div class="container">
22: <div class="row">
23: <div class="col-md-6 col-md-offset-3">
24: <a href="/add_book/" class="btn btn-primary">添加书籍</a>
25: <table class="table table-striped table-bordered">
26: <thead>
27: <tr>
28: <th>书籍名称</th>
29: <th>书籍价格</th>
30: <th>书籍出版日期</th>
31: <th>书籍出版社</th>
32: <th>删除</th>
33: <th>编辑</th>
34: </tr>
35: </thead>
36: <tbody>
37: {% for book in book_list %}
38: <tr>
39: <td>{{ book.title}}</td>
40: <td>{{ book.price}}</td>
41: <td>{{ book.pub_date|date:'Y-m-d'}}</td>
42: <td>{{ book.publish}}</td>
43: <td><a href="/find_book/{{ book.id }}/delete/" class="btn btn-danger">删除</a></td>
44: <td><a href="/find_book/{{ book.id }}/update/" class="btn btn-info">编辑</a></td>
45: </tr>
46: {% endfor %}
47: </tbody>
48: </table>
49:
50: </div>
51: </div>
52: </div>
53: </body>
54: </html>
update_book.html
1: <!DOCTYPE html>
2: <html lang="en">
3: <head>
4: <meta charset="UTF-8">
5: <title>Title</title>
6:
7: <link rel="stylesheet" href="/static/dist/css/bootstrap.css">
8: <style>
9: .container{
10: margin-top: 100px;
11: }
12: .btn{
13: margin-top: 10px;
14: }
15: h3{
16: margin-left: 40%;
17: }
18: </style>
19: </head>
20: <body>
21: <h3>添加书籍</h3>
22:
23: <div class="container">
24: <div class="row">
25: <div class="col-md-6 col-md-offset-3">
26: <form action="" method="post">
27: {% csrf_token %}
28: <div>
29: <label for="">书籍名称</label>
30: <input type="text" class="form-control" name="title" value="{{ book_obj.title }}">
31: </div>
32: <div>
33: <label for="">价格</label>
34: <input type="text" class="form-control" name="price" value="{{ book_obj.price }}">
35: </div>
36: <div>
37: <label for="">出版日期</label>
38: <input type="date" class="form-control" name="pub_date" value="{{ book_obj.pub_date|date:'Y-m-d' }}">
39: </div>
40: <div>
41: <label for="">出版社</label >
42: <input type="text" class="form-control" name="publish" value="{{ book_obj.publish }}">
43: </div>
44: <input type="submit" class="btn btn-success pull-right ">
45: </form>
46: </div>
47: </div>
48: </div>
49: </body>
50: </html>
删除则直接跳转到编辑界面。具体效果自行测试,