首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图书管理系统demo1

图书管理系统demo1

作者头像
kirin
发布2020-05-09 15:44:59
3840
发布2020-05-09 15:44:59
举报
文章被收录于专栏:Kirin博客Kirin博客

# 图书管理系统项目 ps开始就遇到了语法错误,找了我10多分钟,最后发现是urls中.用了:导致的错误,太粗心了呀。。。 1.首先我在pycharm中dj目录下新建了一个项目,名字为day06, 然后在里面又添加了book_manage,front,这两个app,然后新建了静态样式目录static,还有模板文件夹templates,然后分别在新建的app中创建了urls.py,用于url的映射 2.接着在setting中设置了数据库信息->databases,并添加了base.css样式文件,放在了static文件中,在setting最末行添加了,STATICFLIES_DIRS=[os.path.join(BASE_URL,’static’)] 用于静态文件确定静态文件能够被模板正确的调用,在DIRS中设置了模板文件夹为创建好的templates, 3.准备工作好了之后就开始上手啦,首先分别以两个app的名字定义了app_name,然后在主urls中添加了url路由,我用的include导入的(比较节省时间,不用写的太麻烦), 下面是app中的urls: from django.urls import path app_name=’book_manage’ from . import views urlpatterns=[ path(”,views.index,name=’index’), path(‘add_book/’,views.add_book,name=’add_book’), path(‘book_detall/<int:book_id>/’,views.book_detall,name=’book_detall’), path(‘delete_book/’,views.delete_book,name=’delete_book’), ]

4.在book_manage中写了下面这些视图函数 from django.shortcuts import render,redirect,reverse from django.db import connection def get_cursor(): return connection.cursor() #定义游标函数 def index(request): pass cursor=get_cursor() #建立游标 cursor.execute(‘select id,name,author from book’) books=cursor.fetchall() return render(request,’index.html’,context={‘books’:books}) # 把查询到的数据返回给index def add_book(request): if request.method==’GET’: # 判断请求是否为post如不是返回添加页面 return render(request,’add_book.html’) else: name=request.POST.get(‘name’) print(name) # 这里因为之前一直只获取到一个参数,另一个一直未none,原来是html页面中name命名多了个单引号,倒是名字不对,所以为空, author=request.POST.get(‘author’) cursor=get_cursor() cursor.execute(”’insert into book(id,name,author) values(null,'{}’,'{}’)”’.format(name,author)) #执行数据库写入操作 return redirect(reverse(‘book_manage:index’)) # 返回首页 def book_detall(request,book_id): cursor=get_cursor()# 书籍详情页 cursor.execute(”’select id,name,author from book where id=%s”’% book_id) # 获取到查询的数据 book=cursor.fetchone() return render(request,’book_detall.html’,context={‘book’:book}) # 渲染后返回给html def delete_book(request): if request.method==’POST’: book_id=request.POST.get(‘book_id’)# 获取到html页面中提交上来的post请求, cursor=get_cursor() cursor.execute(‘delete from book where id=%s’% book_id) 在数据库中执行删除操作 return redirect(reverse(‘book_manage:index’)) else: raise RuntimeError(‘删除图书错误’) ###html文件的话我建立了一个base.html 主模板文件,后面的模板文件直接用 {% extends ‘base.html’%} 继承,然后在body部分溜了个{% block content %}{%endblock%}接口给后面的子模板增加功能,因为整体的页头页脚都是不变的, ##建立的html文件有 base.html # 主模板,用于后面子页继承

{% load static %}

<html lang=”en”> <head> <meta charset=”UTF-8″> <link rel=”stylesheet” href=”{% static ‘base.css’%}”> <meta name=”viewport” content=”width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0″> <meta http-equiv=”X-UA-Compatible” content=”ie=edge”> <title>图书管理系统</title> </head> <body> <nav> <ul class=”nav”> <li><a href=”/”>首页</a></li> <li><a href=”{% url ‘book_manage:add_book’%}”>发布图书</a></li> </ul> </nav> {% block content %} {% endblock %}

</body> </html> index.html # 首页

{% extends ‘base.html’ %} {% block content %} <thead> <tr> <th>序号</th> <th>书名</th> <th>作者</th> </tr> </thead>

<tbody> {% for book in books %} <tr><br> <td>{{ forloop.counter }}</td> <td><a href=”{% url ‘book_manage:book_detall’ book_id=book.0 %}”>{{ book.1 }}</a></td> <td>{{ book.2 }}</td>

</tr> {% endfor %} </tbody>

{% endblock %} 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%} book_detall.html # 图书详情页 {% extends ‘base.html’%} {% block content %} <p>书名:{{ book.1 }}</p> <p>作者:{{ book.2 }}</p> <form action=”{% url ‘book_manage:delete_book’ %}” method=”post”> <input type=”hidden” name=”book_id” value=”{{ book.0}}”> <input type=”submit” value=”删除图书”> </form> {% endblock %}

###整体的结构就是这样的了,噢,差点忘了,还有个css文件 @#base.css .nav{ background:#3a3a3a; height:65px; overflow:hidden; } *{ margin:0; padding:0; } a{text-decoration:none;} .nav li{ float:left; list-style:None; margin:0 20px; line-height:65px; } .nav li a{ color:#fff; } .nav li a:hover{ color:lightblue;}

因为第一次写这种项目,没经验,花的时间比较长,大概2个小时左右,emm多半部分都花在了html页面上。。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档