练习-单表查询操作
项目目录
要求:实现书籍的增加页面;查看书籍;删除书籍;编辑功能
创建项目,创建应用app01;检查:settings的app有无创建的app01的应用名;templates有无DIRS;链接mysql数据库表;创建models表中的类
settings
#1 检查APP有无自创建的app01 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', ] #2 检查templates有无DIRS路径 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] #3 取消原来默认的数据库,创建mysql数据库;数据库名字写上,mysql数据库名字 密码对应上 # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases # DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } # } DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'book_o1', # 要连接的数据库,连接前需要创建好 'USER': 'root', # 连接数据库的用户名 'PASSWORD': '123', # 连接数据库的密码 'HOST': '127.0.0.1', # 连接主机,默认本级 'PORT': 3306 # 端口 默认3306 } }
找到项目名文件下的init,在里面写入:
import pymysql
pymysql.install_as_MySQLdb()
models
from django.db import models # Create your models here. #在建立好的库下,创建Book表名,设置id,title,pub_date,price,publish字段 class Book(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=32) pub_date = models.DateField() price = models.DecimalField(max_digits=8, decimal_places=2) publish = models.CharField(max_length=32)
总结步骤:
- 数据库新建一个库
- settings 修改一下
- makemigrations
- migrate
urls
from django.contrib import admin from django.urls import path,re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path('addbook/',views.addbook), # 添加书籍 path('books/',views.books), # re_path(r"books/(\d+)/delete",views.delbook), #捕获值 re_path(r"books/(\d+)/change",views.changebook), #捕获值 d表示1-9之间数字 ]
views
from django.shortcuts import render,HttpResponse,redirect # Create your views here. from app01.models import Book def addbook(request): if request.method == "POST": title=request.POST.get("title") price = request.POST.get("price") pub_date = request.POST.get("date") publish = request.POST.get("publish") book_obj=Book.objects.create(title=title,price=price,pub_date=pub_date,publish=publish) # 创造书籍对象 return redirect("/books/") return render(request,"addbook.html") def books(request): book_list = Book.objects.all() # 获取所有的书籍对象 return render(request,"books.html",locals()) #返回books 并带所有变量 def delbook(request,id): Book.objects.filter(id=id).delete() return redirect("/books/") def changebook(request,id): book_obj=Book.objects.filter(id=id).first() if request.method == "POST": title=request.POST.get("title") price=request.POST.get("price") date=request.POST.get("date") publish=request.POST.get("publish") Book.objects.filter(id=id).update(title=title,price=price,pub_date=date,publish=publish) return redirect("/books/") return render(request,"changebook.html",{"book_obj":book_obj})
templates/addbook.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bs/css/bootstrap.css"> <style> .container{ margin-top:100px; } .btn{ margin-top: 10px; } </style> </head> <body> <h3>添加书籍</h3> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <form action="" method="post"> {% csrf_token %} <div> <label for="">书籍名称</label> <input type="text" class="form-control" name="title"> </div> <div> <label for="">价格</label> <input type="text" class="form-control" name="price"> </div> <div> <label for="">出版日期</label> <input type="date" class="form-control" name="date"> </div> <div> <label for="">出版社</label> <input type="text" class="form-control" name="publish"> </div> <input type="submit" class="btn btn-success pull-right"> </form> </div> </div> </div> </body> </html>
templates/books.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bs/css/bootstrap.css"> <style> .container{ margin-top:100px; } .btn{ margin-top: 10px; } </style> </head> <body> <h3>查看书籍</h3> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <a href="/addbook/" class="btn btn-primary">添加书籍</a> <table class="table table-striped table-bordered"> <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.title }}</td> <td>{{ book.price }}</td> <td>{{ book.pub_date|date:'Y-m-d' }}</td> <td>{{ book.publish }}</td> <td><a href="/books/{{ book.pk }}/delete" class="btn btn-danger">删除</a></td> <td><a href="/books/{{ book.pk }}/change" class="btn btn-info">编辑</a></td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
templates/changebook.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/bs/css/bootstrap.css"> <style> .container{ margin-top:100px; } .btn{ margin-top: 10px; } </style> </head> <body> <h3>编辑书籍</h3> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <form action="" method="post"> {% csrf_token %} <div> <label for="">书籍名称</label> <input type="text" class="form-control" name="title" value="{{ book_obj.title }}"> </div> <div> <label for="">价格</label> <input type="text" class="form-control" name="price" value="{{ book_obj.price }}"> </div> <div> <label for="">出版日期</label> <input type="date" class="form-control" name="date" value="{{ book_obj.pub_date|date:'Y-m-d' }}"> </div> <div> <label for="">出版社</label> <input type="text" class="form-control" name="publish" value="{{ book_obj.publish }}"> </div> <input type="submit" class="btn btn-success pull-right"> </form> </div> </div> </div> </body> </html>