当前位置:   article > 正文

【Django】第二课 基于Django图书借阅管理网站平台_基于django web的图书管理系统前端登陆注册网页代码

基于django web的图书管理系统前端登陆注册网页代码

概念

本文在上一篇文章之上完成登录,图书显示,关键字搜索以及分页功能

登录功能实现

当用户在首页进行输入学生用户信息后,点击登录按钮发送请求给服务器,地址请求为: /toLogin/

urls.py

path('toLogin/',views.toLogin),

将接收的学号和密码发送给服务器进行查询数据库进行验证,如果该学生不存在,则跳转至登录提示页面,否则进入主页,并将登录的用户信息存储起来

  1. # 保存登录信息
  2. student=[]
  3. def toLogin(request):
  4. # 接收学生的登录信息,并将登录成功的学生信息临时存储
  5. num=request.POST["num"]
  6. psd=request.POST["psd"]
  7. # 根据登录信息查询数据库该学生是否存在
  8. global student
  9. student=Student.objects.filter(number=num,psd=psd)
  10. # 如果能查询到数据,则进入主页
  11. if student:
  12. return redirect(bookList)
  13. else:
  14. return render(request,"loginError.html")

主页地址请求为: 

path('bookList/',views.bookList),

views.py,将数据库中所有图书信息进行查询并做分页处理

  1. def bookList(request):
  2. try:
  3. ym=request.GET["ym"]
  4. except:
  5. ym=1
  6. # 将所有图书信息查询出来
  7. bookList = Book.objects.all().values()
  8. p=paginator.Paginator(bookList,16)
  9. page=p.get_page(ym)
  10. bookList=page.object_list
  11. yms=p.page_range
  12. for b in bookList:
  13. b["image"] = "img/" + str(b["image"])
  14. return render(request, "home.html", {"student": student[0], "bookList": bookList,"page":page,"yms":yms})

top.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. #top_{
  8. width: 100%;
  9. height: 160px;
  10. font-size: 16px;
  11. color: white;
  12. }
  13. #top ul{
  14. list-style: none;
  15. margin: 0px;
  16. padding: 0px;
  17. }
  18. p{
  19. width: 200px;
  20. height: 40px;
  21. line-height: 60px;
  22. margin: 0px auto;
  23. color: white;
  24. font-size: 30px;
  25. font-weight: bold;
  26. }
  27. #top_ ul li{
  28. margin: 0px;
  29. padding: 0px 10px;
  30. width: 80px;
  31. height: 40px;
  32. text-align: center;
  33. }
  34. .li_name{
  35. float: left;
  36. }
  37. .li_{
  38. float: right;
  39. }
  40. #top_ ul li a{
  41. text-decoration: none;
  42. color: white;
  43. }
  44. #top_ ul li a:hover{
  45. color: red;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="top">
  51. {% load static %}
  52. <div id="top_" style="background: url('{% static 'img/bg.jpeg' %}')">
  53. <ul>
  54. <li class="li_name"><a href="">
  55. <span style="color: #0077ff">{{ student.name }}</span>
  56. 同学</a></li>
  57. <li class="li_"><a href="/">注销</a></li>
  58. <li class="li_"><a href="/hisInfo/">历史记录</a></li>
  59. <li class="li_"><a href="/MyJY/">我的借阅</a></li>
  60. <li class="li_"><a href="/bookList/">返回首页</a></li>
  61. </ul>
  62. <p id="p_">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
  63. </div>
  64. </div>
  65. </body>
  66. </html>

主页的html代码如下:

  1. <!DOCTYPE html >
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>首页</title>
  6. {% load static %}
  7. <link rel="stylesheet" type="text/css" href="{% static 'css/css1.css' %}">
  8. <style type="text/css">
  9. #h_content {
  10. width: 100%;
  11. margin: 10px 0px;
  12. }
  13. #h_content_top {
  14. width: 100%;
  15. height: 60px;
  16. }
  17. #seach {
  18. width: 400px;
  19. margin: auto;
  20. }
  21. #seach form {
  22. width: 380px;
  23. margin: 0px auto;
  24. }
  25. #seach form .t_seach {
  26. width: 300px;
  27. height: 30px;
  28. border-radius: 3px;
  29. border-width: 3px;
  30. border-color: chocolate;
  31. }
  32. #seach form .btn_seach {
  33. width: 60px;
  34. height: 30px;
  35. border-radius: 3px;
  36. border-width: 0px;
  37. background-color: #00aaff;
  38. color: white;
  39. }
  40. #h_content_center {
  41. width: 100%;
  42. }
  43. #h_content_center ul {
  44. list-style: none;
  45. width: 900px;
  46. margin: 0px;
  47. padding: 0px;
  48. }
  49. #h_content_center ul li {
  50. width: 200px;
  51. float: left;
  52. margin: 5px;
  53. }
  54. #h_content_center ul li img {
  55. width: 200px;
  56. margin: 0px auto;
  57. }
  58. #h_content_center ul li p {
  59. width: 100%;
  60. text-align: center;
  61. color: black;
  62. font-size: 11px;
  63. }
  64. #h_content_center a {
  65. text-decoration: none;
  66. color: black;
  67. }
  68. #h_content_center a:LINK {
  69. color: black;
  70. }
  71. #h_content_center a:HOVER {
  72. color: red;
  73. }
  74. #h_content_foot ul {
  75. margin: 0px;
  76. padding: 0px;
  77. list-style: none;
  78. }
  79. #h_content_foot ul li {
  80. width: 20px;
  81. height: 20px;
  82. font-size: 12px;
  83. float: left;
  84. margin: 10px;
  85. text-align: center;
  86. }
  87. #h_content_foot ul li:HOVER {
  88. color: white;
  89. background-color: #0077ff;
  90. }
  91. #nav{
  92. width: 900px;
  93. height: 40px;
  94. margin-top: 20px;
  95. clear: left;
  96. }
  97. #nav a{
  98. text-decoration: none;
  99. margin: 3px;
  100. background-color: cadetblue;
  101. }
  102. #nav a:hover{
  103. background-color: #FF1719;
  104. }
  105. #nav a.c_page{
  106. background-color: #FF1719;
  107. }
  108. #nav a.up_page,#nav a.do_page{
  109. width: 80px;
  110. height: 40px;
  111. padding: 5px 10px;
  112. border-radius: 5px;
  113. color: white;
  114. }
  115. #nav a.p_page{
  116. width: 40px;
  117. height: 40px;
  118. padding: 3px;
  119. border-radius: 5px;
  120. color: white;
  121. }
  122. </style>
  123. <script type="text/javascript">
  124. //定义方法,用于处理分页导航栏的样式
  125. function a_style() {
  126. //通过class选择器获得分页导航栏对象
  127. var aElements=document.getElementsByClassName("p_page");
  128. for (var i = 0; i < aElements.length; i++) {
  129. var text=aElements.item(i).innerHTML;
  130. if (text<10){
  131. aElements.item(i).innerHTML="&nbsp;"+text+"&nbsp;";
  132. }
  133. }
  134. }
  135. </script>
  136. </head>
  137. <body onload="a_style()">
  138. <div id="h_body"
  139. style="width: 900px;
  140. height: 800px;margin: 10px auto;">
  141. <!-- 头部:登录的用户,历史记录,我的借阅,注销 -->
  142. {% include "top.html" with stu=student %}
  143. <!-- 内容 -->
  144. <div id="h_content">
  145. <div id="h_content_top">
  146. <div id="seach">
  147. <form action="/seach/" method="post">
  148. {% csrf_token %}
  149. <input type="text" class="t_seach" width="80" height="40"
  150. name="seach" placeholder="根据书名搜索,例如:java"> &nbsp;&nbsp;<input
  151. type="submit" class="btn_seach" name="submit" value="搜索">
  152. </form>
  153. </div>
  154. </div>
  155. <div id="h_content_center">
  156. <ul>
  157. {% for foo in bookList %}
  158. <li ><a href="/bookInfo/?bookid={{ foo.id }}"><img alt="{{ foo.bookName }}"
  159. src="{% static foo.image %}"></a>
  160. <p><a href="/bookInfo/?bookid={{ foo.id }}">{{ foo.bookName }}</a></p>
  161. </li>
  162. {% endfor %}
  163. </ul>
  164. </div>
  165. <!-- 显示页码导航栏 -->
  166. <div id="nav" align="center">
  167. <!-- 上一页 -->
  168. <!-- 判断当前页是否有上一页,如果有上一页则显示上一页的按钮,否则就不显示上一页 -->
  169. {% if page.has_previous %}
  170. <a href="/bookList/?ym={{ page.previous_page_number }}" class="up_page">上一页</a>
  171. {% endif %}
  172. <!-- 页码 -->
  173. {% for ym in yms %}
  174. {% if page.number == ym %}
  175. <a href="/bookList/?ym={{ ym }}" class="p_page c_page">{{ ym }}</a>
  176. {% else %}
  177. <a href="/bookList/?ym={{ ym }}" class="p_page">{{ ym }}</a>
  178. {% endif %}
  179. {% endfor %}
  180. <!-- 下一页 -->
  181. {% if page.has_next %}
  182. <a href="/bookList/?ym={{ page.next_page_number }}" class="do_page">下一页</a>
  183. {% endif %}
  184. </div>
  185. </div>
  186. <!-- 底部 -->
  187. {% include "foot.html" %}
  188. </div>
  189. </body>
  190. </html>

其界面效果如下

根据关键字查询图书信息

在搜索文本框中输入要搜索的书籍名称的关键字,点击搜索后,发出请求  /seach/

urls.py中进行匹配

path('seach/',views.seach),

 views.py

  1. def seach(request):
  2. info=request.POST["seach"]
  3. # 进行模糊查询
  4. bookList=Book.objects.filter(bookName__contains=info).values()
  5. try:
  6. ym = request.GET["ym"]
  7. except:
  8. ym = 1
  9. p = paginator.Paginator(bookList, 16)
  10. page = p.get_page(ym)
  11. bookList = page.object_list
  12. yms = p.page_range
  13. for b in bookList:
  14. b["image"] = "img/" + str(b["image"])
  15. return render(request, "home.html", {"student": student[0], "bookList": bookList, "page": page, "yms": yms})

其页面效果如下

查看图书详情信息功能实现

当用户需要查看某一本图书的时候,进行选择对应的图书信息点击,想服务器发送请求 /bookInfo/

 urls.py文件中定义该地址

path('bookInfo/',views.bookInfo),

views.py文件中接收用户发送的图书id进行获取图书信息,以及该用户是否对该图书有借阅记录的状态获取

  1. def bookInfo(request):
  2. bookId=request.GET["bookId"]
  3. # 将图书编号作为条件查询该图书的详细信息
  4. book=Book.objects.get(id=bookId)
  5. # 修改图书的图片路径
  6. book.image="img/"+str(book.image)
  7. # 查询借阅信息表当前这本书是否有学生已经借走了
  8. infos=UserBookInfo.objects.filter(number=student[0].number,
  9. bookId=bookId)
  10. # 判断当前这本书的状态
  11. if infos:
  12. state=2
  13. for i in infos:
  14. if i.state == 1:
  15. state=i.state
  16. else:
  17. state=0
  18. return render(request,"bookInfo.html",{"student":student[0],
  19. "book":book,
  20. "state":state})

并显示在图书详细html页面上,其代码如下

  1. <!DOCTYPE html >
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>书籍详情</title>
  6. {% load static %}
  7. <link rel="stylesheet" type="text/css" href="{% static 'css/css1.css' %}">
  8. <style type="text/css">
  9. #b_content input{
  10. width: 200px;
  11. height: 40px;
  12. border-radius: 5px;
  13. color: white;
  14. font-size: 18px;
  15. font-weight: bold;
  16. }
  17. </style>
  18. <script type="text/javascript">
  19. function a_() {
  20. alert("该书已借阅,不能同时借阅同一本书!!!");
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <div id="body">
  26. <!-- 头部 -->
  27. {% include "top.html" with stu=student %}
  28. <!-- 内容 -->
  29. <div id="b_content">
  30. <table>
  31. <tr >
  32. <td rowspan="5" width="30%" align="center">
  33. <img alt="封面" title="封面"
  34. src="{% static book.image %}"> </td>
  35. <td width="70%"><b>书籍名称:</b>&nbsp;&nbsp;{{ book.bookName }}</td>
  36. </tr>
  37. <tr>
  38. <td><b>作者:</b>&nbsp;&nbsp;{{ book.author }}</td>
  39. </tr>
  40. <tr>
  41. <td><b>出版日期:</b>&nbsp;&nbsp;{{ book.bookDate }}</td>
  42. </tr>
  43. <tr>
  44. <td><b>出版社:</b>&nbsp;&nbsp;{{ book.bookAddress }}</td>
  45. </tr>
  46. <tr>
  47. <td><b>所属分类:</b>&nbsp;&nbsp;{{ book.type }}</td>
  48. </tr>
  49. <tr>
  50. <td colspan="2"><b>内容简介:</b>
  51. <br>
  52. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ book.info }}</td>
  53. </tr>
  54. <tr>
  55. <td colspan="2" align="center">
  56. {% if state == 0 or state == 2 %}
  57. <a href="/addUserBookInfo/?bookid={{ book.id }}">
  58. <input type="button" style="background-color: #0077ff;" value="借阅">
  59. </a>
  60. {% elif state == 1 %}
  61. <a onclick="a_()">
  62. <input type="button" style="background-color: red;" value="已借阅">
  63. </a>
  64. {% endif %}
  65. </td>
  66. </tr>
  67. </table>
  68. </div>
  69. <!-- 底部 -->
  70. {% include "foot.html" %}
  71. </div>
  72. </body>
  73. </html>

其界面效果如下

如果当前学生对某图书有借阅则不允许再对该书进行借阅

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/734814
推荐阅读
相关标签
  

闽ICP备14008679号