1 查询操作练习
1 查询老男孩出版社出版过的价格大于200的书籍 2 查询2017年8月出版的所有以py开头的书籍名称 3 查询价格为50,100或者150的所有书籍名称及其出版社名称 4 查询价格在100到200之间的所有书籍名称及其价格 5 查询所有人民出版社出版的书籍的价格(从高到低排序,去重)
def abc(request):
res1 = Book.objects.filter(publish="老男孩出版社", price__gt=200)
res2 = Book.objects.filter(pub_date__year=2017, pub_date__month=8, title__startswith="py").values("title")
res3 = Book.objects.filter(price__in=[50, 100, 150]).values("title", "publish")
res4 = Book.objects.filter(price__range=[100, 200]).values("title", "price")
res5 = Book.objects.filter(publish="人民出版社").order_by("-price").values("price").distinct()
print(res1, res2, res3, res4, res5)
return HttpResponse("OK")
2 图书管理系统
实现功能:book单表的增删改查
models.py
from django.db import models # Create your models here. class Book(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8, decimal_places=2) pub_date = models.DateField() publish = models.CharField(max_length=32)
views.py
from django.shortcuts import render, redirect from app01.models import Book from django.urls import reverse # Create your views here. def check(request): res = Book.objects.all().values("id", "title", "price", "pub_date", "publish") return render(request, "check.html", locals()) def add(request): if request.method == 'GET': return render(request, "add.html") Book.objects.create( title=request.POST.get("title"), price=request.POST.get("price"), publish=request.POST.get("publish"), pub_date=request.POST.get("pub_date"), ) url = reverse("check") return redirect(url) def delete(request, number): Book.objects.filter(id=number).delete() url = reverse("check") return redirect(url) def edit(request, number): if request.method == 'GET': res = Book.objects.filter(id=number).values("id", "title", "price", "pub_date", "publish") return render(request, "edit.html", locals()) Book.objects.filter(id=number).update( title=request.POST.get("title"), price=request.POST.get("price"), publish=request.POST.get("publish"), pub_date=request.POST.get("pub_date"), ) url = reverse("check") return redirect(url)
orm_lx.__init__.py
import pymysql pymysql.install_as_MySQLdb()
settings.py
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', ], }, }, ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'orm1', 'USER': 'root', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': 3306 } } STATICFILES_DIRS = [ os.path.join(BASE_DIR, "statics"), ]
orm_lx.urls.py
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path("check/", views.check, name="check"), path("add/", views.add, name="add"), path("delete/<int:number>/", views.delete), path("edit/<int:number>", views.edit), ]
add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加书籍</title> <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css"> <script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script> <script src="/static/jQuery3.6.js"></script> </head> <body> <div class="container"> <h3>添加书籍</h3> <p></p> <div class="row"> <div class="col-md-4 col-md-offset-2"> <form action="{% url 'add' %}" method="post"> {% csrf_token %} <p> <label for="i1"> 书籍名称: <input type="text" name="title" class="form-control" id="i1"> </label> </p> <p> <label for="i2"> 价格: <input type="text" name="price" class="form-control" id="i2"> </label> </p> <p> <label for="i3"> 出版社: <input type="text" name="publish" class="form-control" id="i3"> </label> </p> <p> <label for="i4"> 出版日期: <input type="date" name="pub_date" class="form-control" id="i4"> </label> </p> <p> <input type="submit" value="提交" class="btn btn-primary pull-left"> </p> </form> </div> </div> </div> </body> </html>
check.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查看书籍</title> <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css"> <script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script> <script src="/static/jQuery3.6.js"></script> <style> a { color: white; } a:hover { text-decoration: none; color: orange; } </style> </head> <body> <div class="container"> <h3>查看书籍</h3> <div class="row"> <div class="col-md-8 col-md-offset-2"> <button class="btn btn-info"><a href="{% url 'add' %}">添加书籍</a></button> <p></p> <table class="table table-striped text-center table-bordered"> <thead> <tr> <th class="text-center">书籍名称</th> <th class="text-center">价格</th> <th class="text-center">出版日期</th> <th class="text-center">出版社</th> <th class="text-center">删除操作</th> <th class="text-center">编辑操作</th> </tr> </thead> <tbody> {% for i in res %} <tr> <td>{{ i.title }}</td> <td>{{ i.price }}</td> <td>{{ i.pub_date|date:"Y-m-d" }}</td> <td>{{ i.publish }}</td> <td> <button class="btn btn-danger btn-sm"><a href="/delete/{{ i.id }}">删除</a></button> </td> <td> <button class="btn btn-warning btn-sm"><a href="/edit/{{ i.id }}">编辑</a></button> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
edit.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑书籍</title> <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css"> <script src="/static/bootstrap-3.4.1-dist/js/bootstrap.min.js"></script> <script src="/static/jQuery3.6.js"></script> </head> <body> <div class="container"> <h3>编辑书籍</h3> <p></p> <div class="row"> <div class="col-md-4 col-md-offset-2"> {% for res in res %} <form action="/edit/{{ res.id }}" method="post"> {% csrf_token %} <p> <label for="i1"> 书籍名称: <input type="text" name="title" class="form-control" id="i1" value={{ res.title }}> </label> </p> <p> <label for="i2"> 价格: <input type="text" name="price" class="form-control" id="i2" value={{ res.price }}> </label> </p> <p> <label for="i3"> 出版社: <input type="text" name="publish" class="form-control" id="i3" value={{ res.publish }}> </label> </p> <p> <label for="i4"> 出版日期: <input type="date" name="pub_date" class="form-control" id="i4" value={{ res.pub_date|date:"Y-m-d" }}> </label> </p> <p> <input type="submit" value="提交" class="btn btn-primary pull-left"> </p> </form> {% endfor %} </div> </div> </div> </body> </html>