使用django创建一个单表查询的图书管理系统
生活随笔
收集整理的這篇文章主要介紹了
使用django创建一个单表查询的图书管理系统
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用django創(chuàng)建一個單表查詢的圖書管理系統(tǒng)
在settings.py文件中添加(用于連接mysql數(shù)據(jù)庫)
DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'djangotest','HOST': '127.0.0.1','PORT': 3306,'USER': 'root','PASSWORD': '123'} }在init.py文件中添加(替換默認(rèn)的MySQLdb)
import pymysql pymysql.install_as_MySQLdb()在models.py文件中添加創(chuàng)建表和字段的語句
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)在終端中執(zhí)行創(chuàng)建的命令
python3 manage.py makemigrations python3 manage.py migrate配置路由urls.py
from django.conf.urls import url from django.contrib import admin from books import viewsurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^index/', views.login),url(r'^$', views.login), ]寫視圖函數(shù)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>圖書管理系統(tǒng)</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>查詢結(jié)果:</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>轉(zhuǎn)載于:https://www.cnblogs.com/jianhaozhou/p/9910816.html
總結(jié)
以上是生活随笔為你收集整理的使用django创建一个单表查询的图书管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用decimal模块增加python的浮
- 下一篇: 关系型数据库全表扫描分片详解