django中图书管理系统中的增删改查功能的代码实现
时间: 2023-05-12 19:02:56 浏览: 179
简单的图书管理系统,增删改查,用LISt实现,java新手看
以下是一个简单的 Django 图书管理系统的增删改查功能的代码实现:
1. 增加图书:
```python
from .models import Book
def add_book(request):
if request.method == 'POST':
title = request.POST['title']
author = request.POST['author']
publisher = request.POST['publisher']
pub_date = request.POST['pub_date']
book = Book(title=title, author=author, publisher=publisher, pub_date=pub_date)
book.save()
return redirect('book_list')
return render(request, 'add_book.html')
```
2. 删除图书:
```python
from .models import Book
def delete_book(request, book_id):
book = Book.objects.get(id=book_id)
book.delete()
return redirect('book_list')
```
3. 修改图书:
```python
from .models import Book
def edit_book(request, book_id):
book = Book.objects.get(id=book_id)
if request.method == 'POST':
book.title = request.POST['title']
book.author = request.POST['author']
book.publisher = request.POST['publisher']
book.pub_date = request.POST['pub_date']
book.save()
return redirect('book_list')
return render(request, 'edit_book.html', {'book': book})
```
4. 查询图书:
```python
from .models import Book
def book_list(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})
```
以上是一个简单的 Django 图书管理系统的增删改查功能的代码实现。
阅读全文