美文网首页
Django之图书管理系统(1)

Django之图书管理系统(1)

作者: wangcc_sd | 来源:发表于2019-03-21 23:48 被阅读0次

    路由文件,此文件放置路由及对应函数。

    urls.py

    """book URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.1/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('addbook/',views.addbook),
        path('books/',views.books),
    ]
    
    

    views.py

    在这里放置主要的视图逻辑代码,并通过render方法返回到相应的html文件中去。

    from django.shortcuts import render,HttpResponse
    
    # Create your views here.
    from app01.models import Book
    
    #添加书籍
    def addbook(request):
        if request.method=='POST':
            title = request.POST.get('title')
            price = request.POST.get('price')
            date = request.POST.get('date')
            publish = request.POST.get('title')
            print(title,price,date,publish)
            book_obj=Book.objects.create(title=title,price=price,pub_date=date,publish=publish)
            return HttpResponse(r'ok')
        return render(request,'addbook.html')
    
    def books(request):
        book_list=Book.objects.all()
        return render(request,'books.html',locals())
    

    models.py

    这里主要放置book这个类,作用类似于数据库连接对象

    from django.db import models
    
    # Create your models here.
    class Book(models.Model):
        id = models.AutoField(primary_key=True)  # 主键
        title = models.CharField(max_length=32)  # 字符串
        pub_date = models.DateField()            # 时间类型
        price = models.DecimalField(max_digits=8, decimal_places=2)  # 浮点型
        publish = models.CharField(max_length=32)
    
        def __str__(self):
            return self.title
    

    addbook.html

    这里放置的是添加书籍的html页面,这里主要是样式渲染及页面内容显示。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>addbook</title>
        <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
        <style>
            .container{
                margin-top: 100px;
            }
            .btn{
                margin-top:10px;
            }
        </style>
    </head>
    <body>
    <h3>添加书籍</h3>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <form action="" method="post">
                    {% csrf_token %}
                    <div>
                        <label for="">书籍名称</label>
                        <input type="text" class="form-control" name="title">
                    </div>
                    <div>
                        <label for="">价格</label>
                        <input type="text" class="form-control" name="price">
                    </div>
                    <div>
                        <label for="">出版日期</label>
                        <input type="date" class="form-control" name="date">
                    </div>
                    <div>
                        <label for="">出版社</label>
                        <input type="text" class="form-control" name="publish">
                    </div>
                    <input type="submit" class="btn btn-success pull-right" >
    
                </form>
    
            </div>
        </div>
    </div>
    
    
    
    </body>
    </html>
    

    books.html

    这里放置的是书籍查询页面,这里的内容现在还不完善

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>addbook</title>
        <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
        <style>
            .container{
                margin-top: 100px;
            }
            .btn{
                margin-top:100px;
            }
        </style>
    </head>
    <body>
    <h3>查看书籍</h3>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
               <table class="table-striped table-bordered">
                    <thead>
                    <tr>
                    <th>书籍名称</th>
                    <th>价格</th>
                    <th>出版日期</th>
                    <th>出版社</th>
                        <th>删除操作</th>
                        <th>编辑操作</th>
                    </tr>
                    </thead>
                   <thead>
                   {% for book in book_list %}
                       <tr>
                       <td>{{ book.title }}</td>
                        <td>{{ book.price }}</td>
                        <td>{{ book.pub_date|date:'Y-m-d' }}</td>
                        <td>{{ book.publish }}</td>
                        <td><a href="" class="btn btn-danger">删除</a></td>
                       </tr>
                   {% endfor %}
                   </thead>
               </table>
            </div>
        </div>
    </div>
    
    
    
    </body>
    </html>
    

    这个图书管理系统讲会整合django与数据库交互的所有逻辑,类似于一个加强巩固的过程,加油!


    1278547.jpg

    相关文章

      网友评论

          本文标题:Django之图书管理系统(1)

          本文链接:https://www.haomeiwen.com/subject/oghkvqtx.html