Django----ORM增删改查

ORM的增删改查

文章目录

    • ORM的增删改查
      • 添加数据
      • 查询数据
      • 删除数据
      • 修改数据
      • 基于ORM的书籍查询

from django.shortcuts import render
from django.http import HttpResponse

# 从模型中导入这个类
from .models import Book

添加数据

def add_book(request):
    """增加图书"""
    # 添加数据的时候要对应着从模型中导入的类数据字段的一致性
    # 实例化对象,并添加数据
    book = Book(book_name='利用Python进行数据分析', book_author='Wes Mckinney', book_price=100)
    # 写完之后记得要保存进行保存数据
    # save 保存数据
    book.save()
    return HttpResponse('添加图书数据成功')

查询数据

def find_book(request):
    """查询图书操作
    思路解说:
    为什么要使用pk,pk其实是primary_key 的单词缩写意思
    假设我们创建了一个对应的模型,当我们创建的模型的主键不叫id了,叫随便的单词。
    这时候我们在对应的查询过程中就会出现报错的情况,如果我们使用pk就不用考虑这点问题,而且还不会报错
    """
    # get:是根据主键来进行查询
    # book = Book.objects.get(pk=1)
    # print(book)

    # 根据其他条件进行查询
    # filter是根据其他条件进行查询
    # book = Book.objects.filter(book_name='Python3网络爬虫开发实战')
    # print(book)

    # all是查询除所有的数据
    books = Book.objects.all()
    for book in books:
        print(book)
    return HttpResponse('查询图书数据成功')

删除数据

def delete_book(request):
    """对图书进行删除操作"""
    # 先找到id在进行删除
    book = Book.objects.get(pk=8)
    # delete 为删除的意思
    book.delete()
    return HttpResponse('删除图书成功')

修改数据

def update_book(request):
    """对图书进行修改操作"""
    book = Book.objects.get(pk=3)
    book.book_name = 'Python数据分析'
    book.save()
    return HttpResponse('更新图书数据成功')

基于ORM的书籍查询

Django----ORM增删改查_第1张图片

Django----ORM增删改查_第2张图片

Django----ORM增删改查_第3张图片

front/views.py

from django.shortcuts import render, redirect, reverse
from django.http import HttpResponse
from .models import Book

# Create your views here.


def index(request):
    # 使用ORM模型来进行操作
    books = Book.objects.all()
    context = {
        'books': books
    }
    return render(request, 'index.html', context=context)


def add_book(request):
    if request.method == 'POST':
        name = request.POST.get('book_name')
        author = request.POST.get('book_author')
        book = Book(book_name=name, author=author)
        book.save()
        return redirect(reverse('front:index'))

    else:
        return render(request, 'add_book.html')


def book_detail(request, book_id):
    book = Book.objects.get(pk=book_id)
    content = {
        'book': book
    }
    return render(request, 'book_detail.html', context=content)


def book_delete(request, book_id):
    if book_id:
        book = Book.objects.get(pk=book_id)
        book.delete()
        return redirect(reverse('front:index'))
    else:
        return render(request, 'book_delete.html')

front/urls.py

# @Time : 2020/6/28 14:14
# @Author : SmallJ

from . import views
from django.urls import path

app_name = 'front'


urlpatterns = [
    path("", views.index, name='index'),
    path("add_book/", views.add_book, name='add_book'),
    path("book_detail//", views.book_detail, name='book_detail'),
    path('book_delete//', views.book_delete, name='book_delete')
]

add_book.html

{% extends 'base.html' %}

{% block content %}
<form action="" method="post">

    {% csrf_token %}
    图书名字:<input type="text" name="book_name"><br>
    图书作者:<input type="text" name="book_author"><br>
    <input type="submit" value="提交">
form>
{% endblock %}

base.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <ul>

        <li><a href="{% url 'front:index' %}">首页a>li>
        <li><a href="#">发布图书a>li>

    ul>
    {% block content %}
    {% endblock %}
body>
html>

book_detail.html

{% extends 'base.html' %}

{% block content %}
<table>
        <tr>
            <th>序号th>
            <th>图书名字th>
            <th>图书作者th>
            <th>操作/删除th>
        tr>

        <tr>
            <td>{{ book.pk }}td>
            <td>{{ book.book_name }}td>
            <td>{{ book.author }}td>
            <td><a href="{% url 'front:book_delete' book_id=book.pk %}">删除图书a>td>
        tr>

table>
{% endblock %}

index.html

{% extends 'base.html' %}

{% block content%}
    <ul>
        <li><a href="{% url 'front:add_book' %}">添加图书</a></li>
    </ul>
    <table>
        <tr>
            <th>序号</th>
            <th>图书名字</th>
            <th>图书作者</th>
        </tr>
        {% for book in books %}
            <tr>
                <td>{{ forloop.counter }}</td>
                <td><a href="{% url 'front:book_detail' book_id=book.id %}">{{ book.book_name }}</a></td>
                <td>{{ book.author }}</td>
            </tr>
        {% endfor %}
    </table>
{% endblock %}

你可能感兴趣的:(Django框架)