当前位置: 首页 > news >正文

培训学校管理制度大全深圳网站优化怎么做

培训学校管理制度大全,深圳网站优化怎么做,杭州建设企业网站,夺宝网站是怎么做推广的Django 5.2.3 构建的图书管理系统 下面是一个使用 Django 5.2.3 构建的图书管理系统实现方案,包含模型设计、后台管理、基本视图和模板: 1. 环境准备 安装部分请先看 Django 5 学习笔记总纲-CSDN博客 安装部分 pip install django5.2.3 django-admin s…

Django 5.2.3 构建的图书管理系统

下面是一个使用 Django 5.2.3 构建的图书管理系统实现方案,包含模型设计、后台管理、基本视图和模板:

 1. 环境准备

安装部分请先看

Django 5 学习笔记总纲-CSDN博客 安装部分


pip install django==5.2.3
django-admin startproject library
cd library
python manage.py startapp books

2. 模型设计 (`books/models.py`)


from django.db import models
from django.urls import reverseclass Author(models.Model):name = models.CharField(max_length=100)birth_date = models.DateField(null=True, blank=True)bio = models.TextField(blank=True)def __str__(self):return self.namedef get_absolute_url(self):return reverse('author_detail', args=[str(self.id)])class Book(models.Model):GENRE_CHOICES = [('FIC', '小说'),('SCI', '科幻'),('HOR', '恐怖'),('HIS', '历史'),('BIO', '传记'),]title = models.CharField(max_length=200)author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')isbn = models.CharField('ISBN', max_length=13, unique=True)genre = models.CharField(max_length=3, choices=GENRE_CHOICES)publish_date = models.DateField()stock = models.PositiveIntegerField(default=0)description = models.TextField(blank=True)def __str__(self):return f"{self.title} ({self.author.name})"def get_absolute_url(self):return reverse('book_detail', args=[str(self.id)])

3. 后台管理配置 (`books/admin.py`)


from django.contrib import admin
from .models import Author, Book@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):list_display = ('name', 'birth_date')search_fields = ('name',)date_hierarchy = 'birth_date'@admin.register(Book)
class BookAdmin(admin.ModelAdmin):list_display = ('title', 'author', 'genre', 'stock')list_filter = ('genre', 'publish_date')search_fields = ('title', 'isbn', 'author__name')raw_id_fields = ('author',)date_hierarchy = 'publish_date'ordering = ('-publish_date',)

4. URL 配置 (`library/urls.py`)

4.1urls.py


from django.contrib import admin
from django.urls import path, include
from books import viewsurlpatterns = [path('admin/', admin.site.urls),path('books/', include('books.urls')),
]

4.2创建 `books/urls.py`


from django.urls import path
from . import viewsurlpatterns = [path('', views.BookListView.as_view(), name='book_list'),path('<int:pk>/', views.BookDetailView.as_view(), name='book_detail'),path('authors/', views.AuthorListView.as_view(), name='author_list'),path('authors/<int:pk>/', views.AuthorDetailView.as_view(), name='author_detail'),
]

5. 视图实现 (`books/views.py`)

```python
from django.views.generic import ListView, DetailView
from .models import Book, Authorclass BookListView(ListView):model = Booktemplate_name = 'books/book_list.html'context_object_name = 'books'paginate_by = 10def get_queryset(self):queryset = super().get_queryset()# 添加搜索功能if 'search' in self.request.GET:search_term = self.request.GET['search']queryset = queryset.filter(title__icontains=search_term)return querysetclass BookDetailView(DetailView):model = Booktemplate_name = 'books/book_detail.html'class AuthorListView(ListView):model = Authortemplate_name = 'books/author_list.html'context_object_name = 'authors'class AuthorDetailView(DetailView):model = Authortemplate_name = 'books/author_detail.html'def get_context_data(self, **kwargs):context = super().get_context_data(**kwargs)context['books'] = self.object.books.all()return context
```

6. 模板文件

`templates/books/book_list.html`:

<!DOCTYPE html>
<html>
<head><title>图书列表</title>
</head>
<body><h1>图书管理系统</h1><form method="get"><input type="text" name="search" placeholder="搜索书名..."><button type="submit">搜索</button></form><ul>{% for book in books %}<li><a href="{{ book.get_absolute_url }}">{{ book.title }}</a>({{ book.author.name }}) - 库存: {{ book.stock }}</li>{% endfor %}</ul>{% include "pagination.html" %}
</body>
</html>

templates/books/book_detail.html

<!DOCTYPE html>
<html>
<head><title>{{ book.title }}</title>
</head>
<body><h1>{{ book.title }}</h1><p>Author: {{ book.author }}</p><p>Publication date: {{ book.publication_date }}</p>
</body>
</html>

http://www.15wanjia.com/news/157620.html

相关文章:

  • 互联网之光博览会预约乐陵关键词seo
  • ssc网站开发wordpress去掉rss订阅
  • 网站建设技术合作合同网站二级目录 修改路径
  • 口红机网站怎么做的巴中网站建设培训班
  • 怎样做软件网站建设net网站开发做手工简笔
  • 奉贤宜昌网站建设线上推广引流渠道
  • 崇州市建设局网站什么是搜索引擎
  • 天津网站建设方案托管英文版wordpress安装
  • html5制作手机网站做任务能赚钱的网站有哪些
  • 十几万 建设网站类似直播平台网站的建设费用
  • 重庆网站排名响应式网站模板是什么
  • 网站建设公司兴田德润i优惠吗贵州省建设学校官方网站
  • 哪些网站专门做动漫的上海网站域名注册价格
  • 国外优秀电商网站人武部正规化建设经验
  • 学校网站建设主体广州市建设交易服务中心网站
  • 做电子商务网站的总结seo优化网站推广全域营销获客公司
  • 网站建设开发价格怎么算公司主页网站怎么做
  • 网站建设方案平台选择公司网站运营方案
  • 做app 的模板下载网站制作设计图的网页
  • 建网站要多少钱一个网站后台上传图片脚本错误
  • 网站被封了怎么办网站如何上线
  • 网站流量指标有哪些什么是网站平台开发
  • 行业网站制作河北省住房建设厅网站
  • 汽车网站建设多少钱网页设计与制作教程题
  • led 网站模板网站收录慢
  • 找人做网站流程wordpress改模版
  • 携程网站 建设平台分析discuz修改网站底部
  • 网站建设与维护 课件微信公众号如何发布wordpress
  • 小型IT网站开发公司丽江网站建设c3sales
  • 帮别人做网站维护违法学室内设计好不好