044~045:图书管理系统案例

这两节开始做个简单的图书管理系统(基于43节课程学的原生Django使用原生SQL操作):

主要实现如下功能:图书查看(包括详情)、添加和删除:

1、创建一个book_manage工程(使用pycharm创建时,同时创建一个APP——cms),url情况:

2、setting.py中静态文件加载,MySQL数据库配置,同时关闭中间件的csrf功能:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
注释:MIDDLEWARE 中间件的csrf:
# 'django.middleware.csrf.CsrfViewMiddleware',

#MySQL 数据库配置

DATABASES = {

  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test',
    'USER': 'root',
    'PASSWORD': '123456789',
    'HOST': '127.0.0.1',
    'PORT': '3306'
  }
}

3、静态文件模板——base.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/css/index.css">
</head>
<body>
    <nav>
        <ul class="nav">
            <li><a href="/">首页</a></li>
            <li><a href="{% url 'add_book' %}">发布图书</a></li>
        </ul>
    </nav>
{% block content %}{% endblock %}
</body>
</html>

4、index.css 内容:

* {
    margin: 0;
    padding: 0;
}

.nav{
    background: #3a3a3a;
    height: 65px;
    overflow: hidden;
}

.nav li{
    float: left;
    list-style: none;
    margin: 0 20px;
    line-height: 65px;
}

.nav li a{
    color: #fff;
    text-decoration: none;
}

.nav li a:hover{
    color: lightblue;
}

5、首页文件内容:

{% extends 'base.html' %}
{% block content %}
    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>书名</td>
                <td>作者</td>
            </tr>
        </thead>
        <tbody>
            {% for book in books %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td><a href="{% url 'book_detail' book_id=book.0 %}">{{ book.1 }}</a></td>
                    <td>{{ book.2 }}</td>
                </tr>
            {% endfor %}

        </tbody>
    </table>
{% endblock %}

6、views.py文件内容:

from django.shortcuts import render, redirect, reverse
from django.db import connection

# Create your views here.
def get_cursor():
    return connection.cursor()

def index(request):
    cursor = get_cursor()
    cursor.execute("select * from book")
    books = cursor.fetchall()
    return render(request, 'index.html', {'books': books})

def add_book(request):
    if request.method == 'GET':
        return render(request, 'add_book.html')
    else:
        name = request.POST.get('name')
        author = request.POST.get('author')
        cursor = get_cursor()
        cursor.execute("insert into book (name, auther) VALUES ('%s', '%s')" % (name, author))
        return redirect(reverse('index'))


def book_detail(request, book_id):
    cursor = get_cursor()
    cursor.execute("select * from book where id=%s" % book_id)
    book = cursor.fetchone()
    # print(book[0],book[1],book[2])
    print(type(book))
    return render(request, 'book_detail.html', {'book': book})

def del_book(request):
    if request.method == "POST":
        book_id = request.POST.get("book_id")
        cursor = get_cursor()
        cursor.execute("delete from book where id=%s" % book_id)
        return redirect(reverse(index))
    else:
        raise RuntimeError("删除图书失败!!!")

7、add_book.html文件内容:

{% extends 'base.html' %}

{% block content %}
    <form action="" method="post">
    <table>
        <tbody>
            <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="submit" value="添加"></td>
            </tr>
        </tbody>
    </table>
    </form>
{% endblock %}

8、book_detail.html文件内容:

{% extends 'base.html' %}

{% block content %}
    <p>书名:{{ book.1 }}</p>
    <p>作者:{{ book.2 }}</p>
    <form action="{% url 'del_book' %}" method="post">
        <input type="hidden" name="book_id" value="{{ book.0 }}">
        <input type="submit" value="删除">
    </form>
{% endblock %}

 

转载于:https://www.cnblogs.com/zheng-weimin/p/10206249.html

图书馆管理系统使用说明书 配置源程序 附加MYSQL数据库 将TM\02\ Database\db_librarySys文件夹拷贝到mysql\data文件夹下即可,如图1.1所示。 图1.1 拷贝MYSQL数据库 将程序发布到Tomcat下 (1)将“TM\02”文件夹拷贝到Tomcat安装路径下的webapps文件夹中。 (2)选择开始菜单中的“所有程序\Apache Tomcat 6.0\Monitor Tomcat”命令,这时在windows的系统托盘中会显示标识Tomcat服务器启动状态的图标,如果显示为 ,则说明Tomcat服务器没有启动,这时可以在该图标上单击鼠标右键在弹出的快捷菜单中选择“Start Service”菜单项启动Tomcat服务器,启动后将显示为 。 (3)打开IE浏览器,在地址栏中输入http://localhost:8080/,进入“Tomcat软件管理”页面。 注意:8080为安装Tomcat时设置的端口号。 (4)单击Tomcat Manager超链接,弹出“连接到 localhost”对话框。 (5)在用户名及密码处输入登录Tomcat的用户名和密码,单击【确定】按钮。 (6)进入“Tomcat应用程序管理”页面,在此页面中单击“02”,进入本程序主页面,完成Tomcat配置。 导入所应用的包 在运行本程序时,需要将Struts 1.2和MySQL数据库驱动包拷贝到Tomcat安装路径下的webapps文件夹中的02\WEB-INF\lib文件夹中。 使用说明 系统介绍 图书馆管理系统主要的目的是实现图书馆的信息化管理。图书馆的主要业务就是新书的借阅和归还,因此系统最核心的功能便是实现图书的借阅和归还。此外,还需要提供图书的信息查询、读者图书借阅情况的查询等功能。项目实施后,能够提高图书馆的图书借阅、归还流程,提高工作效率。整个项目需要在两个月的时间内交付用户使用。 操作注意事项 (1)本系统的用户名为:tsoft,密码为:111 (2)读者类型不同,可借图书的本数也有所区别。 操作流程 (1)用户登录图书馆管理系统后,可看到图书借阅排行榜,通过排行榜可以看出借阅图书的名称、图书类型、借阅次数等相关信息。 (2)单击“系统设置”/“图书馆信息”命令,对图书馆信息进行设置操作。 (3)单击“系统设置”/“管理员设置”命令,对管理员信息进行添加、权限设置、查询及删除操作。 (4)单击“系统设置”/“参数设置”命令,对办证费用及有效期限信息进行添加操作。 (5)单击“系统设置”/“书架设置”命令,对书架信息进行添加、修改及删除操作。 (6)单击“读者管理”/“读者类型管理”命令,对读者类型信息进行添加、修改及删除操作。 (7)单击“读者管理”/“读者档案管理”命令,对读者信息进行添加、修改及删除操作。 (8)单击“图书管理”/“图书类型设置”命令,对图书类型信息进行添加、修改及删除操作。 (9)单击“图书管理”/“图书档案管理”命令,对图书信息进行添加、修改及删除操作。 (10)单击“图书借还”/“图书借阅”命令,对图书借阅信息添加操作。 (11)单击“图书借还”/“图书续借”命令,对图书续借信息进行添加操作。 (12)单击“图书借还”/“图书归还”命令,对图书归还信息进行添加操作。 (13)单击“系统查询”/“图书档案查询”命令,对图书档案信息进行查询操作。 (14)单击“系统查询”/“图书借阅查询”命令,对借阅的图书信息进行查询操作。 (15)单击“系统查询”/“借阅到期提醒”命令,对借阅到期提醒信息进行查询操作。 (16)单击“更改口令”按钮,对当前的用户密码进行修改操作。 (17)单击“退出系统”按钮,退出当前操作系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值