ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ORM单表操作练习

2021-11-22 18:33:08  阅读:125  来源: 互联网

标签:title Book price 练习 request publish django ORM 单表


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>

 

标签:title,Book,price,练习,request,publish,django,ORM,单表
来源: https://www.cnblogs.com/xuewei95/p/15589761.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有