使用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>

转载于:https://www.cnblogs.com/jianhaozhou/p/9910816.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值