Go教程

Django框架图书管理之查看书籍与添加书籍

本文主要是介绍Django框架图书管理之查看书籍与添加书籍,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

 

 

from django.shortcuts import render,HttpResponse
from book.models import Book
import datetime
# Create your views here.
def add_book(request):

    if request.method == "GET":
        return render(request, "add_book.html")
    else:
        #方式一
        #title =request.POST.get("title")
        #price = request.POST.get("price")
        #pub_date = request.POST.get("pub_date")
        #Book.objects.create(title=title,price=price,pub_date=pub_date)
        #方式二
        #request.POST.dict()
        Book.objects.create(**request.POST.dict())

        return redirect("/book/select")
        #return HttpResponse("添加书籍")


    #添加书籍
    # 方式1
    #date = datetime.date(year=2012,month=12,day=12)
    #book = Book(title="xiyouji", price=199, pub_date=date)
    book = Book(title="xiyouji",price=199,pub_date="2012-12-12")
    print(book.id) #None
    book.save() #执行sql
    print(book.id) # 3

    # 方式2
    book = Book.objects.create(title="sanguoyanyi",price=299,pub_date="2011-11-11")
    print(book.id)
    print(book.price)

def select_book(request):

    #return render(request,"add_book.html")
    # (1) 查询所有的书籍,返回值是一个queryset类型对象
    book_list= Book.objects.all()
    '''
    ORM数据引擎:
    
    select id, title, price, pub_date from book:
    +------+--------------+---------+------------+
    id  title             price     pub_date
    +------+--------------+---------+------------+
   西游记            499.00     2011-12-12
   三国演义          399.00     2019-06-12
   水浒传            299.00     2008-06-12
   红楼梦            199.00     2020-06-12
    +------+--------------+---------+------------+
    book01 = Book(1 | 西游记  |499.00 |2011-12-12)
    book02 = Book(2 | 三国演义  |399.00  |2019-06-12)
    book03 = Book(3 | 水浒传    |299.00  |2008-06-12)
    book04 = Book(4 | 红楼梦    |199.00  |2020-06-12)
    
    book_list = queryset[book01,book02,book03,book04]    
    
    '''
    print ("book_list",book_list) # <QuerySet [<Book: Book object (1)>,...]

    #QuerySet:支持索引操作,支持遍历
    book = book_list[0]
    print(book.id) # 1
    print(book.title) # 西游记
    print(book.price) # 499.00
    # print(book_list.title)
    #for book in book_list:
        #print (book.id,book.title)  #1 西游记 2 三国演义 3 水浒传 4 红楼梦

    #return HttpResponse("添加书籍")
    return render(request,"books.html",{"book_list":book_list})

 

 

{% extends "base.html" %}

{% block title %}
<form action="">
    <div class="from-group">
        <lable for="">书籍名称</lable>
        <input type="text" class="form-control">
    </div>
    <div class="from-group">
        <lable for="">书籍价格</lable>
        <input type="text" class="form-control">
    </div>
    <div class="from-group">
        <lable for="">出版日期</lable>
        <input type="date" class="form-control">
    </div>
    <div class="from-group">
        <input type="submit" class="btn" success pull-right>
    </div>
</form>
{% endfor %}

 

 

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,unique=True) #字符串
    price = models.DecimalField(max_digits=8,decimal_places=2) #浮点数
    pub_date = models.DateField() #日期类型年月日
    pub_dates = models.DateTimeField #日期类型精确到时分秒

    class Meta:
        db_table = "book"

 

 

{% extends "base.html" %}

{% block title %}
<title>书籍</title>
{% endblock %}

{% block content %}
<p>
    <a href="/book/add_book" class="btn btn-primary">添加书籍</a>
</p>

<table>
    <tr>
        <th>序号</th>
        <th>书籍名称</th>
        <th>书籍价格</th>
        <th>书籍出版日期</th>
    </tr>
    {% for book in book_list %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ book.title }}</td>
            <td>{{ book.price }}</td>
            <td>{{ book.pub_date|date:"Y-m-d" }}</td>
        </tr>
{% endfor %}
</table>

这篇关于Django框架图书管理之查看书籍与添加书籍的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!