创建一个简单的 Django 图书馆图书管理系统,我们可以按照以下模块来实现:
下面我们逐步实现这些模块。
首先,创建一个 Django 项目并启动应用:
django-admin startproject library_management_system
cd library_management_system
python manage.py startapp library
在 library 应用的 models.py 文件中,定义图书模型(Book Model)、图书馆模型(Library Model)和图书借阅模型(Borrowing Model):
# library/models.py
from django.db import models
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()
    def __str__(self):
        return self.title
class Library(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=200)
    def __str__(self):
        return self.name
class Borrowing(models.Model):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    library = models.ForeignKey(Library, on_delete=models.CASCADE)
    borrow_date = models.DateField()
    return_date = models.DateField()
    def __str__(self):
        return f"{self.book.title} - {self.library.name}"
运行以下命令来进行数据库迁移:
python manage.py makemigrations
python manage.py migrate
运行以下命令来创建超级用户:
python manage.py createsuperuser
按照提示设置用户名、电子邮件和密码。
在 library 应用的 urls.py 文件中,定义 URL 配置:
# library/urls.py
from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
    path('books/', views.BookListView.as_view(), name='book_list'),
    path('book/<int:pk>/', views.BookDetailView.as_view(), name='book_detail'),
    path('libraries/', views.LibraryListView.as_view(), name='library_list'),
    path('library/<int:pk>/', views.LibraryDetailView.as_view(), name='library_detail'),
]
在 library 应用的 views.py 文件中,编写视图函数:
# library/views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Book, Library
def index(request):
    return render(request, 'index.html')
class BookListView(ListView):
    model = Book
    template_name = 'book_list.html'
    context_object_name = 'books'
class BookDetailView(DetailView):
    model = Book
    template_name = 'book_detail.html'
    context_object_name = 'book'
class LibraryListView(ListView):
    model = Library
    template_name = 'library_list.html'
    context_object_name = 'libraries'
class LibraryDetailView(DetailView):
    model = Library
    template_name = 'library_detail.html'
    context_object_name = 'library'
在 library 应用中创建 templates 文件夹,并在其中创建以下模板文件:
a. index.html :
<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Library Management System</title>
</head>
<body>
    <h1>Welcome to the Library Management System</h1>
    <p>Explore the available books and libraries.</p>
</body>
</html>
b. book_list.html :
<!-- templates/book_list.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Book List</title>
</head>
<body>
    <h1>Book List</h1>
        {% for book in books %}*   <a href="{% url 'book_detail' book.pk %}">{{ book.title }}</a>
</body>
</html>
c. book_detail.html :
<!-- templates/book_detail.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ book.title }} Details</title>
</head>
<body>
    <h1>{{ book.title }} Details</h1>
    <p><strong>Title:</strong> {{ book.title }}</p>
    <p><strong>Author:</strong> {{ book.author }}</p>
    <p><strong>Publication Date:</strong> {{ book.publication_date }}</p>
</body>
</html>
d. library_list.html :
<!-- templates/library_list.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Library List</title>
</head>
<body>
    <h1>Library List</h1>
        {% for library in libraries %}*   <a href="{% url 'library_detail' library.pk %}">{{ library.name }}</a>
</body>
</html>
e. library_detail.html :
<!-- templates/library_detail.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ library.name }} Details</title>
</head>
<body>
    <h1>{{ library.name }} Details</h1>
    <p><strong>Name:</strong> {{ library.name }}</p>
    <p><strong>Address:</strong> {{ library.address }}</p>
</body>
</html>
运行开发服务器:
运行以下命令来启动 Django 开发服务器:
python manage.py runserver
现在,您可以在浏览器中访问 http://127.0.0.1:8000/ 来查看图书馆图书管理系统。您可以在网站上浏览图书列表、图书馆列表,并查看每本图书和每个图书馆的详细信息。
请注意,这只是一个简单的实现,图书管理系统通常需要更多功能和复杂性,例如用户认证、借阅管理、管理员权限等。以上代码仅供演示基