圖書管理系統

一、建立app以後註冊css

settings.pyhtml

 二、templates文件夾建立以及配置文件中的路徑配置前端

settings.pyjquery

三、django鏈接MySQL數據庫

1.配置文件配置django

2.__init__文件中書寫下面兩行代碼
bootstrap

 四、靜態文件配置app

1.手動建立static文件夾ide

2. html頁面上動態解析接口前綴oop

 

 五、建立類(建表)並執行數據庫遷移命令

from django.db import models

# Create your models here.

class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.CharField(max_length=32)
    publish_time = models.DateField(auto_now_add=True)

    publish = models.ForeignKey(to='Publish')
    authors = models.ManyToManyField(to='Author')



class Publish(models.Model):
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=32)

class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.CharField(max_length=64)

    author_detail = models.OneToOneField(to='AuthorDetail')

class AuthorDetail(models.Model):
    phone = models.BigIntegerField()
    addr = models.CharField(max_length=32)
models.py
from django.shortcuts import render,reverse,redirect
from app01 import models
# Create your views here.

def home(request):
    return render(request,'home.html')

def show_book(request):
    #獲取書籍表中全部的數展現到前端
    book_queryset = models.Book.objects.all()
    return render(request,'book_list.html',locals())

def add_book(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish')
        authors_list = request.POST.getlist('authors')
        #操做數據庫
        #書籍表
        book_obj = models.Book.objects.create(title=title,price=price,publish_time=publish_time,publish_id=publish_id)

        #書籍與做者關係表
        book_obj.authors.add(*authors_list)

        # 重定向到書籍展現頁
        return redirect(reverse('show_book'))
    #將網站出版社和做者查詢出來展現到前端頁面供用戶選擇
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request,'add_book.html',locals())

def edit_book(request,edit_id):
    #先獲取用戶想要編輯的對象,展現給用戶看,用戶在基於原書籍進行修改
    edit_obj = models.Book.objects.filter(pk=edit_id).first()
    if request.method =='POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish')
        authors_list = request.POST.getlist('authors')
        models.Book.objects.filter(pk=edit_id).update(title=title,price=price,publish_time=publish_time,publish_id=publish_id)
        edit_obj.authors.set(authors_list)
        return redirect(reverse('show_book'))

    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request,'edit_book.html',locals())
    pass

def delete_book(request,delete_id):
    models.Book.objects.filter(pk=delete_id).delete()
    #重定向到書籍展現頁
    return redirect(reverse('show_book'))
views.py
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #首頁
    url(r'^$',views.home,name='home'),
    #書籍展現頁
    url(r'^book_list/',views.show_book,name='show_book'),
    #書籍添加頁
    url(r'^add_book/',views.add_book,name='add'),
    #書籍編輯頁
    url(r'^edit_book/(?P<edit_id>\d+)',views.edit_book,name='edit'),
    url(r'^delete/(\d+)/',views.delete_book,name='delete')
]
urls.py

templates文件夾下的html頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js'%}"></script>
</head>
<body>
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">BMS</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">圖書 <span class="sr-only">(current)</span></a></li>
        <li><a href="#">出版社</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多 <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Hank</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操做 <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<div class="container-fluid"></div>
    <div class="row"></div>
        <div class="col-lg-3">
            <div class="list-group">
              <a href="{% url 'home' %}" class="list-group-item active">首頁</a>
              <a href="{% url 'show_book' %}" class="list-group-item">圖書列表</a>
              <a href="#" class="list-group-item">出版社列表</a>
              <a href="#" class="list-group-item">做者列表</a>
              <a href="#" class="list-group-item">更多操做</a>
            </div>
        </div>
        <div class="col-lg-9">
            <div class="panel panel-primary">
              <div class="panel-heading">
                <h3 class="panel-title">BMS</h3>
              </div>
              <div class="panel-body">
                  {% block content %}
                      <div class="jumbotron">
                      <h1>  歡迎來到圖書管理系統!</h1>
                      <p>...</p>
                      <p><a class="btn btn-primary btn-lg" href="#" role="button">  點我有驚喜!</a></p>
                    </div>
                  {% endblock %}

              </div>
            </div>
        </div>
</body>
</html>
home.html
{% extends 'home.html' %}

{% block content %}
    <div>
        <a href="{% url 'add' %}" class="btn btn-success">新增</a>
    </div>
    <br>
    <table class="table table-hover table-striped">
        <thead>
            <tr>
                <th>序號</th>
                <th>書名</th>
                <th>價格</th>
                <th>出版日期</th>
                <th>出版社</th>
                <th>做者</th>
                <th>操做</th>
            </tr>
        </thead>
        <tbody>
            {% for book_obj in book_queryset %}
                <tr>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ book_obj.title }}</td>
                    <td>{{ book_obj.price }}</td>
                    <td>{{ book_obj.publish_time|date:'Y-m-d'}}</td>
                    <td>{{ book_obj.publish.name }}</td>
                    <td>
                        {% for author_obj in book_obj.authors.all %}
                            {% if forloop.last %}
                                {{ author_obj.name }}
                            {% else %}
                                {{ author_obj.name }},
                            {% endif %}
                        {% endfor %}

                    </td>
                    <td>
                        <a href="{% url 'edit' book_obj.pk %}" class="btn btn-primary btn-sm">編輯</a>
                        <a href="{% url 'delete' book_obj.pk %}" class="btn btn-danger btn-sm">刪除</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}
book_list.html
{% extends 'home.html' %}

{% block content %}
    <h2 class="text-center">添加書籍</h2>
    <form action="" method="post">
        <p>書名:
            <input type="text" name="title" class="form-control">
        </p>
        <p>價格:
            <input type="text" name="price" class="form-control">
        </p>
        <p>出版日期:
            <input type="date" name="publish_time" class="form-control">
        </p>
        <p>出版社:
            <select name="publish" id="" class="form-control">
                {% for publish_obj in publish_queryset %}
                    <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
                
                {% endfor %}
                
            </select>
        </p>
        <p>做者:
            <select name="authors" id="" class="form-control" multiple>
                {% for author_obj in author_queryset %}
                    <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
                
                {% endfor %}
                
            </select>

        </p>
        <input type="submit" class="btn btn-primary pull-right">
    </form>

{% endblock %}
add_book.html
{% extends 'home.html' %}

{% block content %}
    <h2 class="text-center">編輯書籍</h2>
    <form action="" method="post">
        <p>書名:
            <input type="text" name="title" class="form-control" value="{{ edit_obj.title }}">
        </p>
        <p>價格:
            <input type="text" name="price" class="form-control" value="{{ edit_obj.price }}">
        </p>
        <p>出版日期:
            <input type="date" name="publish_time" class="form-control" value="{{ edit_obj.publish_time|date:'Y-m-d' }}">
        </p>
        <p>出版社:
            <select name="publish" id="" class="form-control">
                {% for publish_obj in publish_queryset %}
                    {% if edit_obj.publish == publish_obj %}
                        <option value="{{ publish_obj.pk }} " selected>{{ publish_obj.name }}</option>
                    {% endif %}
                        <option value="{{ publish_obj.pk }} ">{{ publish_obj.name }}</option>
                {% endfor %}

            </select>
        </p>
        <p>做者:
            <select name="authors" id="" class="form-control" multiple>
                {% for author_obj in author_queryset %}
                    {% if author_obj in edit_obj.authors.all %}
                        <option value="{{ author_obj.pk }} " selected>{{ author_obj.name }}</option>
                    {% else %}
                        <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
                    {% endif %}
                {% endfor %}

            </select>

        </p>
        <input type="submit" class="btn btn-warning pull-right">
    </form>

{% endblock %}
edit_book.html

相關文章
相關標籤/搜索