Django项目实践

项目需求

创建一个book管理系统

实现功能

增,删,改,查

环境

C:\Users\lenovo>python3 -m django --version
1.11.6

C:\Users\lenovo>python3
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
 on win32

步骤

1.使用python创建一个cms项目及应用

2.创建static目录存放Jquery文件

3.模板目录下创建3个html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/dist/css/bootstrap.css">

    <style>
        .container{
            margin-top: 100px;
        }
    </style>
</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
        <div style="text-align: center"><h3>IT图书管理系统</h3></div>
            <a href="/add/"><button class="btn btn-primary">添加</button></a>
            <table style="border: 1px" class="table table-hover">
                <tr>
                    <th>编号</th>
                    <th>书名</th>
                    <th>出版日期</th>
                    <th>价格</th>
                    <th>出版社</th>
                    <th>操作</th>
                </tr>
                {% for book_obj in bookList %}
                    <tr>
                        <td>{{ forloop.counter }}</td>
                        <td>{{ book_obj.title }}</td>
                        <td>{{ book_obj.pubDate|date:"Y-m-d" }}</td>
                        <td>{{ book_obj.price }}</td>
                        <td>{{ book_obj.publish }}</td>
                        <td>
                            <a href="/edit/{{ book_obj.id }}"><button class="btn btn-info">编辑</button></a>
                            <a href="/del/{{ book_obj.id }}"><button class="btn btn-danger">删除</button></a>
                        </td>
                    </tr>
                {% endfor %}

            </table>
        </div>
    </div>
</div>

</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <link rel="stylesheet" href="/static/dist/css/bootstrap.css">
</head>
<body>

<div class="modal-dialog" role="document">
      <div class="modal-body">
                <form action="/edit/{{ edit_book.id }}" method="post">
                      <div class="form-group">
                        <h3>编辑图书信息</h3>
                        <label for="name">书名</label>
                        <input type="text" class="form-control item" name="title" value="{{ edit_book.title }}" placeholder="bookname">
                      </div>
                      <div class="form-group">
                        <label for="age">出版日期</label>
                        <input type="date" class="form-control item" name="pubdate" value="{{ edit_book.pubDate|date:'Y-m-d' }}" placeholder="date">
                      </div>
                      <div class="form-group">
                        <label for="classes">价格</label>
                        <input type="text" class="form-control item" name="price" value="{{ edit_book.price }}" placeholder="price">
                      </div>
                        <div class="form-group">
                        <label for="classes">出版社</label>
                        <input type="text" class="form-control item" name="publish" value="{{ edit_book.publish }}" placeholder="出版社">
                      </div>
                      <input class="btn btn-primary" type="submit" value="提交">
                </form>
      </div>
</div>

</body>
</html>
edit.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/dist/css/bootstrap.css">
</head>
<body>

<!-- Modal -->
<div class="modal-dialog" role="document">
      <div class="modal-body">
                <form action="/add/" method="post">
                      <div class="form-group">
                        <h3>添加图书信息</h3>
                        <label for="name">书名</label>
                        <input type="text" class="form-control item" name="title" placeholder="bookname">
                      </div>
                      <div class="form-group">
                        <label for="age">出版日期</label>
                        <input type="date" class="form-control item" name="pubdate" placeholder="date">
                      </div>
                      <div class="form-group">
                        <label for="classes">价格</label>
                        <input type="text" class="form-control item" name="price" placeholder="price">
                      </div>
                        <div class="form-group">
                        <label for="classes">出版社</label>
                        <input type="text" class="form-control item" name="publish" placeholder="publish">
                      </div>
                      <input class="btn btn-primary" type="submit" value="提交">
                </form>
      </div>
</div>
</body>
</html>
add.html

4.配置urls控制器,通过url匹配views的函数

5.配置views视图函数

from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

from app01 import models
def index(request):
    # 查询所有的书籍
    bookList=models.Book.objects.all() # 返回值QuerySet    [obj1,obj2....]
    return render(request,"index.html",{"bookList":bookList})

def add(request):

    if request.method=="POST":
        print(request.POST)
        title=request.POST.get("title")
        pubdate=request.POST.get("pubdate")
        price=request.POST.get("price")
        publish=request.POST.get("publish")

        # 插入数据
        models.Book.objects.create(title=title,pubDate=pubdate,price=price,publish=publish)

        return redirect("/index/")
    return render(request,"add.html")


def delBook(request,id):

    models.Book.objects.filter(id=id).delete()

    return redirect("/index/")

def editBook(request,id):
    if request.method=="POST":
        title = request.POST.get("title")
        pubdate = request.POST.get("pubdate")
        price = request.POST.get("price")
        publish = request.POST.get("publish")
        print (title,pubdate,price,publish)
        models.Book.objects.filter(id=id).update(title=title,pubDate=pubdate,price=price,publish=publish)
        return redirect("/index/")
    edit_book=models.Book.objects.filter(id=id)[0]      # 返回值QuerySet    [obj1,]
    return render(request,"edit.html",{"edit_book":edit_book})
views.py

6.配置model.py和数据库交互

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)
    pubDate=models.DateField()
    price=models.DecimalField(max_digits=6,decimal_places=2)
    publish=models.CharField(max_length=32)
model.py

7.在setting.py里面配置/static/路径

 

posted @ 2017-11-12 12:42  warren1236  阅读(152)  评论(0)    收藏  举报