91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

一、圖書管理系統

發布時間:2020-07-20 00:07:34 來源:網絡 閱讀:251 作者:a120518129 欄目:編程語言

一、圖書管理系統增刪改查

導入bootstrap的三種方式:

1、首先在django項目中的settings.py中設置:

STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,'static')
]

2、在html文件中的三種方式:bootstrap依賴與jquery,所以jquery要寫在bootstrap前面

方式一:

<head>
<meta charset="UTF-8">
<script src="/static/jquery-3.3.1.js"></script>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
</head>

方式二:

<head>
<meta charset="UTF-8">
<script src="/static/jquery-3.3.1.js"></script>
{% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.css' %}">
</head>

方式三:

<head>
<meta charset="UTF-8">
<script src="/static/jquery-3.3.1.js"></script>
{% load static %}
    <link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css">
</head>

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views
# from app01.views import book_delete
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.book_list),
    url(r'^book_list/$', views.book_list,name='book_list'),
    url(r'^book_add/$', views.book_add),
    url(r'^author_add/$', views.author_add),
    url(r'^publish_add/$', views.publish_add),
    url(r'^author_list/$', views.author_list),
    url(r'^publish_list/$', views.publish_list),
    url(r'^book_delete/(?P<id>\d+)$',views.book_delete,name='book_delete'),
    url(r'^author_delete/$',views.author_delete),
    url(r'^author_update/(\d+)$',views.author_update),
    # url(r'^publish_delete/(?P<id>\d+)$',views.publish_delete),
    url(r'^publish_delete/$',views.publish_delete),
    url(r'^book_update/(?P<id>\d+)$',views.book_update),
    url(r'^publish_update/(\d+)$',views.publish_update),
    url(r'',views.error),
]

views.py

from django.shortcuts import render, redirect, reverse, HttpResponse
# Create your views here.
from app01 import models


def book_list(request):
    book_list = models.Book.objects.all()
    return render(request, 'book_list.html', {"book_list": book_list})


def book_update(request, id):
    if request.method == "GET":
        book = models.Book.objects.get(pk=id)
        authors = models.Author.objects.all()
        publish_list = models.Publish.objects.all()
        return render(request, 'book_update.html', locals())
    else:
        name = request.POST.get('name')
        price = request.POST.get('price')
        pub_date = request.POST.get('pub_date')
        authors = request.POST.getlist('authors')
        publish = request.POST.get('publish')
        # 第一種方式
        book = models.Book.objects.get(pk=id)
        book.name = name
        book.price = price
        book.pub_date = pub_date
        book.publish_id = publish
        book.save()
        # 第二種方式。修改作者
        book.authors.clear()
        book.authors.add(*authors)
        return redirect('/book_list/')

def book_delete(request, id):
    try:
        models.Book.objects.get(pk=id).delete()
    except Exception as e:
        print(e)
    url = reverse('book_list')
    return redirect(url)

def book_add(request):
    if request.method == "GET":
        authors = models.Author.objects.all()
        publish_list = models.Publish.objects.all()
        return render(request, 'book_add.html', locals())
    else:
        name = request.POST.get('name')
        price = request.POST.get('price')
        pub_date = request.POST.get('pub_date')
        authors = request.POST.getlist('authors')
        publish = request.POST.get('publish')
        book = models.Book.objects.create(name=name, price=price, pub_date=pub_date, publish_id=publish)
        # 創建書跟作者的關聯關系
        book.authors.add(*authors)
        return redirect('/book_list/')

def author_list(request):
    authors = models.Author.objects.all()
    return render(request, 'author_list.html', {'author_list': authors})

def publish_list(request):
    publishs = models.Publish.objects.all()
    return render(request, 'publish_list.html', {'publish_list': publishs})

def author_delete(request):
    id = request.GET.get('id')
    ret = models.Author.objects.filter(pk=id).delete()
    return redirect('/author_list/')

def publish_delete(request,*args,**kwargs):
    # print(args,kwargs)
    id = request.GET.get('id')
    # print(id)
    # ret = models.Publish.objects.filter(pk=kwargs["id"]).delete()
    ret = models.Publish.objects.filter(pk=id).delete()
    return redirect('/publish_list/')

def author_update(request, id):
    if request.method == "GET":
        author = models.Author.objects.get(pk=id)
        return render(request, 'author_update.html', locals())
    else:
        name = request.POST.get('name')
        sex = request.POST.get('sex')
        models.Author.objects.filter(pk=id).update(name=name, sex=sex)
        return redirect('/author_list/')

def publish_update(request,id):
    if request.method=="GET":
        publish=models.Publish.objects.get(pk=id)
        return render(request,"publish_update.html",locals())
    else:
        name=request.POST.get("name")
        addr=request.POST.get("addr")
        models.Publish.objects.filter(pk=id).update(name=name,addr=addr)
        return redirect('/publish_list/')

def author_add(request):
    if request.method=="GET":
        return render(request,"author_add.html")
    else:
        name=request.POST.get('name')
        sex=request.POST.get('sex')
        models.Author.objects.create(name=name,sex=sex)
        return redirect('/author_list/')

def publish_add(request):
    if request.method=="GET":
        return render(request,"publish_add.html")
    else:
        name=request.POST.get("name")
        addr=request.POST.get("addr")
        models.Publish.objects.create(name=name,addr=addr)
        return redirect('/publish_list/')

def error(request):
    return HttpResponse('404')

models.py

from django.db import models

# Create your models here.

#用了OneToOneField和ForeignKey,模型表的字段,后面會自定加_id
# ManyToManyField會自動創建第三張表
# *************重點
# 一對一的關系:OneToOneField
# 一對多的關系:ForeignKey
# 多對多的關系:ManyToManyField

class Publish(models.Model):
    # id如果不寫,會自動生成,名字叫nid,并且自增(不同的數據庫,叫的名稱也不一樣)
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    email = models.EmailField()
class Author(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    # 數字類型
    sex = models.CharField(max_length=32)
    # 可以用ForeignKey,但是得設置唯一性約束,會報警告,不建議用,建議用OneToOneField
    # authordetail=models.ForeignKey(unique=True)
    # to='AuthorDetail'  加引號,這個表能找到就可以,不用引號,類必須在上面定義
    authordetail = models.OneToOneField(to='AuthorDetail', to_field='id',null=True)


    def __str__(self):
        return self.name

class AuthorDetail(models.Model):
    id = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)

class Book(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32,db_index=True)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pub_date=models.DateField(auto_now_add=True)
    publish = models.ForeignKey(to=Publish, to_field='id',on_delete=models.CASCADE)
    authors = models.ManyToManyField(to=Author)
    def __str__(self):
        return self.name

templates/author_add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>新增作者</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>新增作者</h2>
            <hr>
            <form action="" method="post">
                <div class="form-group">
                    <p>作者名字:<input type="text" name="name" class="form-control"></p>
                </div>
                <div class="form-group">
                    <p>性別:<input type="text" name="sex" class="form-control"></p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>

templates/author_list.html

{% extends 'base.html' %}

{% block content %}
    <script>
    $('.book').removeClass('active')
    $('.author').addClass('active')
    $('.publish').removeClass('active')
    </script>
    <div class="panel panel-primary">
        <div class="panel-heading">作者列表</div>
        <div class="panel-body">
            <a href="/author_add/" class="btn btn-primary pull-right">新增</a>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>作者名</th>
                    <th>性別</th>
                </tr>
                </thead>
                <tbody>
                {% for author in author_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ author.name }}</td>
                        <td>{{ author.sex }}</td>
                        <td><a href="/author_update/{{ author.pk }}">編輯</a></td>
                        <td><a href="/author_delete/?id={{ author.pk }}">刪除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="panel-footer">
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

templates/author_update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>修改作者</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>修改作者</h2>
            <hr>
            <form action="/author_update/{{ author.pk }}" method="post">
                <div class="form-group">
                    <p>作者姓名:<input type="text" name="name" class="form-control" value="{{ author.name }}"></p>
                </div>
                <div class="form-group">
                    <p>性別:<input type="text" name="sex" class="form-control" value="{{ author.sex }}"></p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>

templates/base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/static/jquery-3.3.1.js"></script>
    {#    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">#}
 {% load static %}
{#            <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.css' %}">#}
 <link rel="stylesheet" href="{% get_static_prefix %}bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>圖書列表</title>
</head>
<body>
<div>
    <nav class="navbar navbar-default navbar-inverse">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
 <div class="navbar-header">
                <a class="navbar-brand" href="#">圖書管理系統</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 navbar-right">
                    <li><a href="#">Link</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
 aria-expanded="false">Dropdown <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>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-3">
            <div class="list-group">
  <a href="/book_list/" class="list-group-item active book">
    圖書列表
 </a>
  <a href="/author_list/" class="list-group-item author">作者列表</a>
  <a href="/publish_list/" class="list-group-item publish">出版社列表</a>
</div>
        </div>
        <div class="col-md-9">
            {% block  content %}
            {% endblock %}
        </div>
    </div>
</div>
</body>
</html>

templates/book_add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>新增圖書</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>新增圖書</h2>
            <hr>
            <form action="" method="post">
                <div class="form-group">
                    <p>書名:<input type="text" name="name" class="form-control"></p>
                </div>
                <div class="form-group">
                    <p>價格:<input type="text" name="price" class="form-control"></p>
                </div>
                <div class="form-group">
                    <p>出版時間:<input type="date" name="pub_date" class="form-control"></p>
                </div>
                <div class="form-group">
                    <p>出版社:
 {#                        <select name="publish">#}
                        {#                            <option value="1">北京出版社</option>#}
                        {#                            <option value="2">上海出版社</option>#}
                        {#                            <option value="3">南京出版社</option>#}
                        {#                        </select>#}
 <select name="publish" class="form-control">
                            {% for publish in publish_list %}
                                <option value="{{ publish.id }}">{{ publish.name }}</option>
                            {% endfor %}
                        </select>
                    </p>
                </div>
                <div class="form-group">
                    <p>作者:
 {#                        <select name="authors">#}
                        {#                            <option value="1">曹雪芹</option>#}
                        {#                            <option value="2">吳承恩</option>#}
                        {#                            <option value="3">曹雪芹</option>#}
                        {#                        </select>#}
 <select name="authors" class="form-control" multiple>
                            {% for author in authors %}
                                    <option value="{{ author.id }}">{{ author.name }}</option>
                            {% endfor %}
                        </select>
                    </p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>

templates/book_list.html

{% extends 'base.html' %}

{% block content %}
    <div class="panel panel-primary">
        <div class="panel-heading"> 圖書列表</div>
        <div class="panel-body">
            <a href="/book_add/" class="btn btn-primary pull-right">新增</a>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>書名</th>
                    <th>價格</th>
                    <th>出版日期</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>編輯</th>
                    <th>刪除</th>
                </tr>
                </thead>
                <tbody>
                {% for book in book_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ book.name }}</td>
                        <td>{{ book.price }}</td>
                        <td>{{ book.pub_date | date:'Y-m-d' }}</td>
                        <td>
                            {% for author in book.authors.all %}
                                {% if forloop.last %}
                                    {{ author.name }}
                                {% else %}
                                    {{ author.name }}|
                                {% endif %}
                            {% endfor %}
                        </td>
                        <td>{{ book.publish.name }}</td>
                        <td><a href="/book_update/{{ book.pk }}">編輯</a></td>
                        <td><a href="/book_delete/{{ book.pk }}">刪除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="panel-footer">
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

templates/book_update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>修改圖書</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>修改圖書</h2>
            <hr>
            <form action="/book_update/{{ book.pk }}" method="post">
                <div class="form-group">
                    <p>書名:<input type="text" name="name" class="form-control" value="{{ book.name }}"></p>
                </div>
                <div class="form-group">
                    <p>價格:<input type="text" name="price" class="form-control" value="{{ book.price }}"></p>
                </div>
                <div class="form-group">
                    <p>出版時間:<input type="date" name="pub_date" class="form-control" value="{{ book.pub_date | date:'Y-m-d'}}"></p>
                </div>
                <div class="form-group">
                    <p>出版社:
 <select name="publish" class="form-control">
                            {% for publish in publish_list %}
                                {% if publish == book.publish %}
                                    <option value="{{ publish.id }}" selected>{{ publish.name }}</option>
                                {% else %}
                                    <option value="{{ publish.id }}">{{ publish.name }}</option>
                                {% endif %}
                            {% endfor %}
                        </select>
                    </p>
                </div>
                <div class="form-group">
                    <p>作者:
 <select name="authors" class="form-control" multiple>
                            {% for author in authors %}
                                {% if author in book.authors.all %}
                                    <option value="{{ author.id }}" selected>{{ author.name }}</option>
                                {% else %}
                                    <option value="{{ author.id }}">{{ author.name }}</option>
                                {% endif %}
                            {% endfor %}
                        </select>
                    </p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>

templates/publish_add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>新增出版社</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>新增出版社</h2>
            <hr>
            <form action="" method="post">
                <div class="form-group">
                    <p>出版社名稱:<input type="text" name="name" class="form-control"></p>
                </div>
                <div class="form-group">
                    <p>出版社地址:<input type="text" name="addr" class="form-control"></p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>

templates/publish_list.html

{% extends 'base.html' %}

{% block content %}
    <script>
    $('.book').removeClass('active')
    $('.author').removeClass('active')
    $('.publish').addClass('active')
    </script>
    <div class="panel panel-primary">
        <div class="panel-heading"> 出版社列表</div>
        <div class="panel-body">
            <a href="/publish_add/" class="btn btn-primary pull-right">新增</a>
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>出版社名稱</th>
                    <th>出版社地址</th>
                </tr>
                </thead>
                <tbody>
                {% for publish in publish_list %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ publish.name }}</td>
                        <td>{{ publish.addr }}</td>

                        <td><a href="/publish_update/{{ publish.pk }}">編輯</a></td>
                        <td><a href="/publish_delete/?id={{ publish.pk }}">刪除</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <div class="panel-footer">
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li>
                        <a href="#" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li><a href="#">1</a></li>
                    <li><a href="#">2</a></li>
                    <li><a href="#">3</a></li>
                    <li><a href="#">4</a></li>
                    <li><a href="#">5</a></li>
                    <li>
                        <a href="#" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
{% endblock %}

templates/publish_update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <title>修改出版社</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>修改出版社</h2>
            <hr>
            <form action="/publish_update/{{ publish.pk }}" method="post">
                <div class="form-group">
                    <p>出版社名稱:<input type="text" name="name" class="form-control" value="{{ publish.name }}"></p>
                </div>
                <div class="form-group">
                    <p>出版社地址:<input type="text" name="addr" class="form-control" value="{{ publish.addr }}"></p>
                </div>
                <input type="submit" value="提交" class="btn btn-primary">
            </form>
        </div>
    </div>
</div>
</body>
</html>


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大石桥市| 唐河县| 遂昌县| 泰顺县| 罗甸县| 泸水县| 隆回县| 广昌县| 桐庐县| 延安市| 林周县| 枣强县| 文山县| 寻乌县| 井研县| 巩义市| 泽普县| 逊克县| 钟山县| 东城区| 漳浦县| 平果县| 琼结县| 布尔津县| 常宁市| 城市| 高平市| 筠连县| 什邡市| 开远市| 西昌市| 习水县| 象山县| 大城县| 嘉善县| 荔浦县| 昌乐县| 台东市| 大邑县| 来凤县| 教育|