Django 操作数据库-原生SQL初体验

2018-12-15  本文已影响0人  前端程序猿

一、安装 mysqlclient

pip install mysqlclient

二、 配置连接数据库

mysite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        # 数据库名
        'NAME': 'django_db1',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

三、使用原生 sql 操作数据库

四、Python DB APIcursor对象,常用接口

五、图书管理系统案例


  1. 使用 Navicat 创建数据库 book_manager

  2. 创建表 book, 三个字段: idtitleauthor

  3. 配置数据库连接
    book_manager/settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            # 数据库名
            'NAME': 'book_manager',
            'USER': 'root',
            'PASSWORD': '123456',
            'HOST': '127.0.0.1',
            'PORT': '3306',
        }
    }
    
  4. 配置路由

    book_manager/urls.py

    from django.urls import path, include
    
    urlpatterns = [
        path('', include('front.urls')),
    ]
    

    front/urls.py

    from django.urls import path
    from . import views
    
    app_name = 'front'
    
    urlpatterns = [
       path('', views.index, name='index'),
       path('create/', views.create_book, name='create'),
       path('detail/<int:book_id>', views.book_detail, name='detail'),
       path('delete/<int:book_id>', views.book_delete, name='delete'),
       path('edit/<int:book_id>', views.book_edit, name='edit'),
    ]
    
  5. 编写视图函数

    front/views.py

    from django.shortcuts import render, reverse, redirect
    from django.db import connection
    
    
    # 获取操作数据库锚点
    def get_cursor():
        return connection.cursor()
    
    # 首页,展示所有图书
    def index(request):
        cursor = get_cursor()
        cursor.execute('select * from book')
        keys = [item[0] for item in cursor.description]
        books = [dict(zip(keys, values)) for values in cursor.fetchall()]
        cursor.close()
        return render(request, 'front/index.html', context={"books": books})
    
    # 新增图书
    def create_book(request):
        if request.method == 'POST':
            title = request.POST.get('title')
            author = request.POST.get('author')
            cursor = get_cursor()
            cursor.execute('insert into book(title, author) values(%s, %s)', (title, author))
            cursor.close()
            return redirect(reverse('front:index'))
        else:
            return render(request, 'front/create.html')
    
    # 图书详情
    def book_detail(request, book_id):
        cursor = get_cursor()
        cursor.execute('select * from book where id=%s', (book_id,))
        keys = [item[0] for item in cursor.description]
        book = dict(zip(keys, cursor.fetchone()))
        cursor.close()
        return render(request, 'front/detail.html', context={"book": book})
    
    # 删除图书
    def book_delete(request, book_id):
        cursor = get_cursor()
        cursor.execute('delete from book where id=%s', (book_id,))
        cursor.close()
        return redirect(reverse('front:index'))
    
    # 编辑图书
    def book_edit(request, book_id):
        cursor = get_cursor()
        if request.method == 'GET':
            cursor.execute('select * from book where id=%s', (book_id,))
            keys = [item[0] for item in cursor.description]
            book = dict(zip(keys, cursor.fetchone()))
            cursor.close()
            return render(request, 'front/edit.html', context={"book": book})
        else:
            title = request.POST.get('title')
            author = request.POST.get('author')
            cursor.execute('update book set title=%s, author=%s where id=%s', (title, author, book_id))
            cursor.close()
            return redirect(reverse('front:index'))
    
  6. static 注册为模板标签
    book_manager/settings.py

    TEMPLATES = [
        {
            'OPTIONS': {
                'builtins': [
                    'django.templatetags.static'
                ],
            },
        },
    ]
    
  7. 关闭表单 csrf 验证
    book_manager/settings.py

    MIDDLEWARE = [
        # 'django.middleware.csrf.CsrfViewMiddleware',
    ]
    
  8. 编写模板
    front/templates/front/base.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>图书管理系统</title>
        <link rel="stylesheet" href="{% static 'front/style.css' %}">
    </head>
    <body>
        {% include 'front/header.html' %}
        {% block content %}
        {% endblock %}
    </body>
    </html>

front/templates/front/header.html

     <header>
        <nav>
            <ul>
                <li><a href="/">首页</a></li>
                <li><a href="/create/">新增图书</a></li>
            </ul>
        </nav>
    </header>

front/templates/front/index.html

    {% extends "front/base.html" %}

    {% block content %}
        <h1>所有图书</h1>
        <table cellpadding="0" cellspacing="0" >
            <thead>
                <tr>
                    <th class="table-id">序号</th>
                    <th class="table-title">书名</th>
                    <th class="table-author">作者</th>
                </tr>
            </thead>
            <tbody>
                {% for book in books %}
                    <tr>
                        <td class="table-id">{{ forloop.counter }}</td>
                        <td class="table-title"><a href="{% url 'front:detail' book_id=book.id %}">{{ book.title }}</a></td>
                        <td class="table-author">{{ book.author }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% endblock %}

front/templates/front/create.html

    {% extends "front/base.html" %}

    {% block content %}
        <h1>新增图书</h1>
        <form action="" method="post" id="create-form">
            <p>
                <label for="title"> 书名:
                    <input type="text" class="input-text" name="title" id="title" required>
                </label>
            </p>
            <p>
                <label for="author"> 作者:
                    <input type="text" class="input-text" name="author" id="author" required>
                </label>
            </p>
            <p>
                <input type="submit" value="新增图书" class="default-btn">
            </p>
        </form>
    {% endblock %}

front/templates/front/detail.html

    {% extends "front/base.html" %}

    {% block content %}
        <h1>图书详情</h1>
        <table cellspacing="0" cellpadding="0">
            {% for key, value in book.items %}
                <tr>
                    <td class="table-id">{{ key }}</td>
                    <td class="table-title">{{ value }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td class="table-id"><button id="book-del" class="small-danger-btn">删除</button></td>
                <td class="table-title"><button id="book-edit" class="small-default-btn">编辑</button></td>
            </tr>
        </table>

        <script>
            const delBtn = document.querySelector('#book-del');
            const editBtn = document.querySelector('#book-edit');

            delBtn.onclick = function() {
                window.location.href = '{% url 'front:delete' book_id=book.id %}'
            };

            editBtn.onclick = function() {
                window.location.href = '{% url 'front:edit' book_id=book.id %}'
            };
        </script>
    {% endblock %}

front/templates/front/edit.html

    {% extends "front/base.html" %}

    {% block content %}
        <h1>编辑图书</h1>
        <form action="{% url 'front:edit' book_id=book.id %}" method="post" id="create-form">
            <p>
                <label for="title"> 书名:
                    <input type="text" class="input-text" value="{{ book.title }}" name="title" id="title" required>
                </label>
            </p>
            <p>
                <label for="author"> 作者:
                    <input type="text" class="input-text" value="{{ book.author }}" name="author" id="author" required>
                </label>
            </p>
            <p>
                <input type="submit" value="编辑图书" class="default-btn">
            </p>
        </form>
    {% endblock %}

front/static/front/style.css

    body, ul, li, h1{
        padding: 0;
        margin: 0;
    }

    nav ul {
        list-style: none;
        height: 60px;
        background-color: #333;
    }

    nav li {
        padding: 0 10px;
        float: left;
        text-align: center;
        line-height: 60px;
    }

    nav a {
        width: 120px;
        display: block;
        height: 60px;
        font-size: 22px;
        color: #fff;
        font-weight: bold;
        text-decoration: none;
    }

    nav a:hover {
        color: lightblue;
    }

    #create-form {
        width: 600px;
        height: 300px;
        background-color: #abcdef;
        margin: 15px auto;
    }

    #create-form p {
        text-align: center;
        padding-top:  25px;
    }

    .input-text {
        width: 200px;
        height: 30px;
        border-radius: 5px;
        border: 1px solid lightblue;
    }

    .default-btn {
        width: 150px;
        height: 50px;
        border-radius: 10px;
        color: #fff;
        background-color: lightgreen;
        line-height: 30px;
        text-align: center;
        border: 1px solid #89a1a9;
    }

    .small-default-btn {
        width: 100px;
        height: 35px;
        border-radius: 10px;
        color: #fff;
        background-color: lightgreen;
        line-height: 30px;
        text-align: center;
        border: 1px solid #89a1a9;
    }

    .small-danger-btn {
        width: 100px;
        height: 35px;
        border-radius: 10px;
        color: #fff;
        background-color: darkred;
        line-height: 30px;
        text-align: center;
        border: 1px solid #89a1a9;
    }

    table {
        margin: 25px auto;
    }

    td, th {
        border: 1px solid lightblue;
        text-align: center;
    }

    .table-id {
        width: 100px;
        height: 50px;
    }

    .table-title {
        width: 500px;
        height: 50px;
    }

    .table-author {
        width: 300px;
        height: 50px;
    }


    h1 {
        text-align: center;
        color: #333;
    }
上一篇 下一篇

猜你喜欢

热点阅读