使用django创建一个单表查询的图书管理系统

使用django创建一个单表查询的图书管理系统

在settings.py文件中添加(用于连接mysql数据库)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangotest',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        'USER': 'root',
        'PASSWORD': '123'
    }
}

在init.py文件中添加(替换默认的MySQLdb)

import pymysql
pymysql.install_as_MySQLdb()

在models.py文件中添加创建表和字段的语句

from django.db import models


# Create your models here.
class Books(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    price = models.FloatField()
    author = models.CharField(max_length=255)
    publish = models.CharField(max_length=255)

在终端中执行创建的命令

python3 manage.py makemigrations
python3 manage.py migrate

配置路由urls.py

from django.conf.urls import url
from django.contrib import admin
from books import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.login),
    url(r'^$', views.login),
]

写视图函数views.py

from books import models


# Create your views here.
def login(request):
    msg = models.Books.objects.all()
    return render(request, 'index.html',{'res_list':msg})

写前端页面index.html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <style>
        body, html, ul, li {
            margin: 0;
            padding: 0;

        }

        ul {
            list-style: none;
        }

        .header_t {
            text-align: center;
            margin: 10px auto;
            line-height: 80px;
        }

        .header {
            width: 100%;
            height: 80px;
            background-color: azure;
        }

        .fd_title {
            background-color: aqua;
            line-height: 60px;
            text-align: center;

        }

        .fd_bd {
            width: 100%;
            height: 400px;
            background-color: chocolate;
        }

        .fd_form {
            margin-left: 90px;
        }

        .fd_form input {
            height: 40px;
            width: 800px;
            font-size: 20px;

        }

        .fd_form button {
            height: 40px;
            width: 80px;
        }

        .fdf_res {
            width: 900px;
            font-size: 18px;
        }

        li {
            float: left;
            height: 28px;
            width: 178px;
            background-color: aqua;
            border: 1px solid black;

            text-align: center;
            line-height: 29px;
        }
        .fdf_res li{
            background-color: snow;
        }
    </style>
</head>
<body>
<div class="header">
    <div class="header_t">
        <h1>图书管理系统</h1>
    </div>
    <div class="find_book">
        <div class="fd_title"><h2>查询书籍</h2></div>
        <div class="fd_bd">
            <div class="fd_form">
                <form action="index.html" method="post">
                    <input type="text" name="bookn" placeholder="请输入书名" AUTOCOMPLETE="off">
                    <button type="submit">提交</button>
                </form>
                <h3>查询结果:</h3>
                <ul>
                    <li>编号</li>
                    <li>书名</li>
                    <li>价格</li>
                    <li>作者</li>
                    <li>版本</li>
                </ul>

                <div class="fdf_res">
                    {% for i in  res_list%}
                        <ul>
                            <li>{{ i.id }}</li>
                            <li>{{ i.name }}</li>
                            <li>{{ i.price }}</li>
                            <li>{{ i.author }}</li>
                            <li>{{ i.publish }}</li>
                        </ul>
                    {% endfor %}

                </div>
            </div>

        </div>
    </div>
</div>

</body>
</html>
posted @ 2018-11-05 18:56  周建豪  阅读(336)  评论(0编辑  收藏  举报