前情提要
Django 已经学了大半.. 很多东西已经能够使用在生产环境当中
一:模糊查询
二:单表删除
三:单表修改
四:图书管理
图书管理操作
视图结构
A:路由层
A :配置路由文件
参数解析:
B :视图层
from django.shortcuts import render, HttpResponse, redirect from django.urls import reverse from app01 import models # Create your views here. def book_list(request): if request.method == "GET": book_list = models.Book.objects.all() # print(book_list) return render(request, "book_list.html", { "booklist": book_list }) else: add_title1 = request.POST.get("title1") add_price1 = request.POST.get("price1") # price add_publish1 = request.POST.get("publish1") add_date1 = request.POST.get("date1") models.Book.objects.create(title=add_title1, price=add_price1, publish=add_publish1, pub_date=add_date1) print(add_title1, add_price1, add_publish1, 111) return render(request, "book_list.html") def bobook_list(request): if request.method == "GET": book_list = models.Book.objects.all() # print(book_list) return render(request, "bo_booklist.html", { "booklist": book_list }) else: add_title1 = request.POST.get("title1") add_price1 = request.POST.get("price1") # price add_publish1 = request.POST.get("publish1") add_date1 = request.POST.get("date1") models.Book.objects.create(title=add_title1, price=add_price1, publish=add_publish1, pub_date=add_date1) print(add_title1, add_price1, add_publish1, 111) return render(request, "bo_booklist.html") def add_book(request): if request.method == "GET": return render(request, "add_book.html") else: add_title = request.POST.get("title") # title add_price = request.POST.get("price") # price add_publish = request.POST.get("publish") add_date = request.POST.get("pub_date") models.Book.objects.create(title=add_title, price=add_price, publish=add_publish, pub_date=add_date) return redirect(reverse("booklist")) def boadd_book(request): if request.method == "GET": return render(request, "boadd_book.html") else: add_title = request.POST.get("title") # title add_price = request.POST.get("price") # price add_publish = request.POST.get("publish") add_date = request.POST.get("pub_date") models.Book.objects.create(title=add_title, price=add_price, publish=add_publish, pub_date=add_date) return redirect(reverse("bobooklist")) def update_book(request, nid): if request.method == "GET": book = models.Book.objects.filter(nid=nid).first() # print(book.title) return render(request, "update_book.html", {"book": book}) else: data = request.POST.dict() del data['csrfmiddlewaretoken'] print(data) models.Book.objects.filter(nid=nid).update(**data) return redirect(reverse("booklist")) def boupdate_book(request, nid): if request.method == "GET": book = models.Book.objects.filter(nid=nid).first() # print(book.title) return render(request, "boupdate.html", {"book": book}) else: data = request.POST.dict() del data['csrfmiddlewaretoken'] print(data) models.Book.objects.filter(nid=nid).update(**data) return redirect(reverse("bobooklist")) def del_book(request,sid): models.Book.objects.filter(nid=sid).delete() return redirect(reverse("bobooklist"))
主要是单表练习.运用了跳转,. 逆向解析. 从html 获取内容,,,,将内容放到html,,从数据库获取内容,,将内容放到数据库,,
C模板层:
book_list
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> 三味书屋</h1> <button><a href="{% url "addbook" %}">新增书籍</a></button> <table border="1"> <tr> <th>序号</th> <th>书名</th> <th>价格</th> <th>出版商</th> <th>日期</th> <th>操作</th> </tr> {% for book in booklist %} <tr> <td>{{forloop.counter}}</td> <td>{{book.title}}</td> <td>{{book.price}}</td> <td>{{book.publish}}</td> <td>{{ book.pub_date |date:"Y-m-d"}}</td> <td><button value="" name=""><a href="{% url "updatebook" nid=book.nid %}">编辑</a></button> <button value="" name=""><a href="{% url "delbook" book.nid %}">删除</a></button> </td> </tr> {% endfor %} </table> <div> <form action="{% url "booklist" %}" method="post"> {% csrf_token %} <div> <span>书名 </span> <input type="text" name="title1" placeholder="请输入书名"><br> <span>价格 </span> <input type="text" name="price1" placeholder="请输入价格"><br> <span>出版商</span> <input type="text" name="publish1" placeholder="请输入出版商"><br> <span>日期 </span> <input type="date" name="date1" placeholder="请输入日期"><br> </div> <div> <button type="submit"><a href="">新增提交</a></button> </div> </form> </div> </body> </html>
add_book
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>add_book</title> </head> <body> <div> <form action="" method="post"> {%csrf_token %} <div> <span>书名 </span> <input type="text" name="title" placeholder="请输入书名"><br> <span>价格 </span> <input type="text" name="price" placeholder="请输入价格"><br> <span>出版商</span> <input type="text" name="publish" placeholder="请输入出版商"><br> <span>日期 </span> <input type="date" name="pub_date" placeholder="请输入日期"><br> </div> <div> <button type="submit" name=""><a href="">新增提交</a></button> </div> </form> </div> </body> </html>
update_book
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> {% csrf_token %} <p>书名 <input type="text" placeholder="{{book.title}}" value="" name="title"></p> <p>价格 <input type="text" placeholder="{{book.price}}" value="" name="price"></p> <p>出版商 <input type="text" placeholder="{{book.publish}}" value="" name="publish"></p> <p>日期 <input type="date" placeholder="{{book.pub_date}}" value="" name="pub_date"></p> <button type="submit">修改</button> </form> </body> </html>
D: 参数文件配置
setting
""" Django settings for dy47 project. Generated by 'django-admin startproject' using Django 2.1.7. For more information on this file, see https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '($z1^$1*b9gi7ydy(=tr7n85v^v7&ks5_pb_kxj)6u3ef12qi@' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'dy47.urls' 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', ], }, }, ] WSGI_APPLICATION = 'dy47.wsgi.application' # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', "NAME": "book2", "HOST": "127.0.0.1", "PROT": 3306, "USER": "root", "PASSWORD": "" } } # Password validation # https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/2.1/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "statics") ]