第四十七篇 图书管理系统之原生SQL实现

心得:调试到半夜的结果,只想要完成它,别无他想

urls.py

from django.urls import path,re_path
from . import views

app_name = 'front'

urlpatterns = [
    path('',views.index,name='index'),
    path('main/',views.main,name='main'),
    path('main/search/',views.search,name='search'),
    path('main/add/',views.add,name='add'),
    path('main/search/(?P<page>)/',views.detail,name='detail'),

]

views.py

from django.shortcuts import render,HttpResponse,reverse,redirect
from django.db import connection
from django.views.decorators.csrf import csrf_exempt

# Create your views here.

@csrf_exempt
def index(request):
    if request.method == 'GET':
        return render(request,'front/index.html')
    else:
        cursor = connection.cursor()
        username=request.POST.get('username')
        password=request.POST.get('password')
        res=cursor.execute("select password from userinfo where username=%s",[username])
        if res != 0:
            db_password=cursor.fetchone()[0] #输出值为元组,所以取第一个值
            if password == db_password:
                return redirect(reverse('front:main'))
            else:
                return render(request, 'front/index.html', context={'error': 'Password Error'})
        else:
            return render(request,'front/index.html',context={'error':'User Not Found'})

def main(request):
    return render(request,'front/main.html')

def search(request):
    cursor=connection.cursor()
    cursor.execute("select id,name from books")
    name=cursor.fetchall()
    return render(request,'front/search.html',context={'name':name})

@csrf_exempt
def detail(request,page):
    if request.method == "GET":
        cursor = connection.cursor()
        cursor.execute("select * from books where id=%s",[page])
        book=cursor.fetchone()
        return render(request,'front/detail.html',context={'book':book})
    else:
        res=request.POST.get('res')
        if res == 'update':
            cursor = connection.cursor()
            name=request.POST.get('name')
            author=request.POST.get('author')
            content = request.POST.get('content')
            cursor.execute("update books set name=%s,author=%s,content=%s where id=%s",[name,author,content,page])
        elif res == 'delete':
            cursor = connection.cursor()
            cursor.execute("delete from books where id=%s",[page])
        return redirect(reverse('front:search'))

@csrf_exempt
def add(request):
    if request.method == "GET":
        return render(request,'front/add.html')
    else:
        cursor = connection.cursor()
        name = request.POST.get('name')
        author = request.POST.get('author')
        content = request.POST.get('content')
        cursor.execute("insert into books(name,author,content) values(%s,%s,%s)",[name,author,content])
        return render(request, 'front/add.html',context={'msg':'添加成功'})

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<h2 style="color:red">{{ error }}</h2>
<br>
<br>

<form action="" method="post">
    {% csrf_token %}
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
    </table>
    <br>
    <input type="submit" value="登录">
</form>

</form>
</body>
</html>

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<h2 style="color:red">{{ error }}</h2>
<br>
<br>

<form action="" method="post">
    {% csrf_token %}
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
    </table>
    <br>
    <input type="submit" value="登录">
</form>

</form>
</body>
</html>

search.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>所有书本信息</title>
</head>
<body>
    <ol>
        {% for i in name %}
        <li><a href="{% url 'front:detail' page=i.0 %}">{{ i.1 }}</a></li>
        {% endfor %}
    </ol>
<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增书籍</title>
</head>
<body>
<h2 style="color:green">{{ msg }}</h2>
<form action="" method="post">
    <table border="1">
        <tr>
            <td>书名:</td>
            <td><input type="text" name="name"></td>
        </tr>
                <tr>
            <td>作者:</td>
            <td><input type="text" name="author"></td>
        </tr>
                <tr>
            <td>内容:</td>
            <td><input type="text" name="content"></td>
        </tr>
    </table>
    <br>
    <input type="submit" value="添加">
</form>
<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>

detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ book.1 }}</title>
</head>
<body>
<h2>书籍详细信息</h2><br>
<form action="" method="post">
    <input type="hidden" name="res" value="update">
    <table border="1">
        <tbody>
            <tr>
                <td>书名:</td>
                <td><input type="text" name="name" value={{ book.1 }}></td>
            </tr>
                    <tr>
                <td>作者:</td>
                <td><input type="text" name="author" value={{ book.2 }}></td>
            </tr>
        <tr>
                <td>内容:</td>
                <td><input type="text" name="content" value={{ book.3 }}></td>
            </tr>
        </tbody>
    </table>
    <br>
    <input type="submit" value="更新">
    </form>
<br>
<form action="" method="post">
    <input type="hidden" name="res" value="delete">
    <input type="submit" value="删除">
</form>

<br>
<br>
<br>
<h3><a href="{% url 'front:main' %}">返回首页</a></h3>
</body>
</html>

图片:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值