project目录
urls.py
from django.contrib import admin
from django.urls import path
from publisher import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.publisher_list),
path('publisher_list/', views.publisher_list),
path('add_publisher/', views.add_publisher),
path('del_publisher/', views.del_publisher),
path('edit_publisher/', views.edit_publisher),
path('book_list/', views.book_list),
path('add_book/', views.add_book),
path('del_book/', views.del_book),
path('edit_book/', views.edit_book),
]
models.py
from django.db import models
class Publisher(models.Model):
pid = models.AutoField(primary_key=True)
name = models.CharField(max_length=32, unique=True)
def __str__(self):
return '{} - {}'.format(self.pid, self.name)
class Book(models.Model):
name = models.CharField(max_length=32, unique=True)
pub = models.ForeignKey('Publisher', on_delete=models.CASCADE)
views.py
def book_list(request):
books = models.Book.objects.all()
return render(request, 'book_list.html', {'books': books})
def add_book(request):
if request.method == 'POST':
book_name = request.POST.get('book_name')
pub_id = request.POST.get('pub_id')
models.Book.objects.create(name=book_name, pub_id=pub_id)
return redirect('/book_list/')
all_publishers = models.Publisher.objects.all().order_by('pid')
return render(request, 'add_book.html', {'all_publishers': all_publishers})
def del_book(request):
book_pk = request.GET.get('pk')
models.Book.objects.filter(id=book_pk).delete()
return redirect('/book_list/')
def edit_book(request):
book_id = request.GET.get('pk')
edit_book_obj = models.Book.objects.get(id=book_id)
if request.method == 'POST':
book_name = request.POST.get('book_name')
pub_id = request.POST.get('pub_id')
edit_book_obj.name = book_name
edit_book_obj.pub_id = pub_id
edit_book_obj.save()
return redirect('/book_list/')
all_publishers = models.Publisher.objects.all().order_by('pid')
return render(request, 'edit_book.html' , {'edit_book_obj': edit_book_obj , 'all_publishers': all_publishers})
add_book.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<p>
书名:<input type="text" name="book_name">
</p>
<p>
出版社:
<select name="pub_id" id="">
{% for all_publisher in all_publishers %}
<option value="{{ all_publisher.pk }}">{{ all_publisher.name }}</option>
{% endfor %}
</select>
</p>
<button>提交</button>
</form>
</body>
</html>
edit_book.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<p>
书名:<input type="text" name="book_name" value="{{ edit_book_obj.name }}">
</p>
<p>
出版社:
<select name="pub_id" id="">
{% for all_publisher in all_publishers %}
{% if all_publisher == edit_book_obj.pub %}
<option selected value="{{ all_publisher.pk }}">{{ all_publisher.name }}</option>
{% else %}
<option value="{{ all_publisher.pk }}">{{ all_publisher.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<button>提交</button>
</form>
</body>
</html>
book_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<thead>
<tr>
<th>序号</th>
<th>ID</th>
<th>书名</th>
<th>出版社</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.pub.name }}</td>
<td>
<a href="/del_book/?pk={{ book.pk }}">删除</a>
<a href="/edit_book/?pk={{ book.pk }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>