模型
from django.db import models
class Book(models.Model):
book_name = models.CharField(max_length=30)
book_publisher = models.CharField(max_length=20)
book_author = models.CharField(max_length=20)
class Meta:
db_table = 'book_tb'
路由控制器
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',views.index),
path('add/',views.add),
path('edit/',views.edit),
path('delete/',views.delete)
]
视图函数
from django.shortcuts import render,HttpResponse,redirect
from django import forms
from app01.models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['book_name','book_publisher','book_author']
def index(request):
books = Book.objects.all()
return render(request,'index.html',{'books': books})
def add(request):
if request.method == 'GET':
b_obj = BookForm()
return render(request,'add.html',{'b_obj': b_obj})
else:
b_obj = BookForm(request.POST)
if b_obj.is_valid():
b_obj.save()
return redirect('/index/')
def edit(request):
id = request.GET.get('id')
book = Book.objects.get(id=id)
if request.method == 'GET':
b_obj = BookForm(instance=book)
return render(request,'edit.html',{'b_obj': b_obj})
else:
b_obj = BookForm(instance=book,data=request.POST)
if b_obj.is_valid():
b_obj.save()
return redirect('/index/')
def delete(request):
id = request.GET.get('id')
book = Book.objects.get(id=id)
book.delete()
return redirect('/index/')
首页模板:index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Index</title>
</head>
<body>
<div style="width: 60%;margin-left: 200px;">
<button><a href="/add/">添加</a></button>
<table style="width: 100%">
<tr>
<th style="border: red 1px solid">ID</th>
<th style="border: red 1px solid">书名</th>
<th style="border: red 1px solid">出版社</th>
<th style="border: red 1px solid">作者</th>
<th style="border: red 1px solid">编辑</th>
<th style="border: red 1px solid">删除</th>
</tr>
{% for book in books %}
<tr>
<td style="border: red 1px solid">{{ book.id }}</td>
<td style="border: red 1px solid">{{ book.book_name }}</td>
<td style="border: red 1px solid">{{ book.book_publisher }}</td>
<td style="border: red 1px solid">{{ book.book_author }}</td>
<td style="border: red 1px solid"><button><a href="/edit/?id={{ book.id }}">编辑</a></button></td> {# 需要传入要编辑的书籍的id #}
<td style="border: red 1px solid"><button><a href="/delete/?id={{ book.id }}">删除</a></button></td> {# 需要传入要删除的书籍的id #}
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
添加页面:add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加页面</title>
</head>
<body>
<div style="width: 60%;margin-left: 200px;">
<form action="" method="post">
{% csrf_token %}
<div>
{{ b_obj.as_p }}
</div>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
编辑页面:edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑页面</title>
</head>
<body>
<div style="width: 60%;margin-left: 200px;">
<form action="" method="post">
{% csrf_token %}
<div>
{{ b_obj.as_p }}
</div>
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
效果:
|