django图书管理系统一

项目截图:

 

 

 

 

 

 

 

 

 

 

 

bookms/settings.py

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'app01.apps.App01Config',

]

 

bookms/urls.py

 

from django.contrib import admin

from django.urls import path,re_path

 

from app01 import views

 

urlpatterns = [

path('admin/', admin.site.urls),

path('addbook/',views.addbook),

path('books/', views.books),

re_path(r"books/(\d+)/delete", views.delbook),

re_path(r"books/(\d+)/change", views.changebook),

]

 

app01/models.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)

pub_date = models.DateField()

price = models.DecimalField(max_length=8,decimal_places=2,max_digits=10)

publish = models.CharField(max_length=32)

 

 

app01/views.py

from django.shortcuts import render,HttpResponse, redirect

 

from .models import Book

# Create your views here.

 

#添加图书视图函数

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("publish")

 

book_obj = Book.objects.create(title=title,price=price,pub_date=date,publish=publish)

 

return redirect("/books/")

 

return render(request, 'app01/addbook.html')

 

#查看所有图书视图函数

def books(request):

book_list = Book.objects.all()

 

return render(request, "app01/books.html",{"book_list":book_list})

 

#删除指定id的视图函数

def delbook(request, id):

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

return redirect("/books/")

 

#编辑指定id的视图函数

def changebook(request, id):

book_obj = Book.objects.filter(id=id).first()

 

if request.method == "POST":

title = request.POST.get("title")

price = request.POST.get("price")

date = request.POST.get("date")

publish = request.POST.get("publish")

 

Book.objects.filter(id=id).update(title=title,price=price,pub_date=date,publish=publish)

return redirect("/books/")

return render(request, "app01/changebook.html",{"book_obj":book_obj})

 

 

 

app01/addbook.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>添加书籍</title>

<link rel="stylesheet" href="/static/dist/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>

 

 

app01/books.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>查看书籍</title>

<link rel="stylesheet" href="/static/dist/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">

<a href="/addbook/" class="btn btn-primary">添加书籍</a>

<table class="table table-striped table-bordered">

<thead>

<tr>

<th>书籍名称</th>

<th>价格</th>

<th>出版日期</th>

<th>出版社</th>

<th>编辑操作</th>

<th>删除操作</th>

</tr>

</thead>

<tbody>

{% 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="/books/{{ book.pk }}/change" class="btn btn-info">编辑</a></td>

<td><a href="/books/{{ book.pk }}/delete" class="btn btn-danger">删除</a></td>

</tr>

{% endfor %}

</tbody>

</table>

</div>

</div>

</div>

</body>

</html>

 

 

app01/changebook.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>编辑书籍</title>

<link rel="stylesheet" href="/static/dist/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" value="{{ book_obj.title }}">

</div>

<div>

<label for="">价格</label>

<input type="text" class="form-control" name="price" value="{{ book_obj.price }}">

</div>

<div>

<label for="">出版日期</label>

<input type="date" class="form-control" name="date" value="{{ book_obj.pub_date|date:'Y-m-d' }}">

</div>

<div>

<label for="">出版社</label>

<input type="text" class="form-control" name="publish" value="{{ book_obj.publish }}">

</div>

 

<input type="submit" class="btn btn-success pull-right">

</form>

</div>

</div>

</div>

</body>

</html>

 

posted @ 2020-04-25 21:47  python坚持者  阅读(1263)  评论(0编辑  收藏  举报