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

上海专业高端网站建奇零seo赚钱培训

上海专业高端网站建,奇零seo赚钱培训,wordpress网站无法登陆,新疆建设兵团104团网站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.yidumall.com/news/32887.html

相关文章:

  • 商业网站的建设上海seo培训中心
  • 毕节网站建设seo引擎优化工具
  • 购物网站开发资源搜索
  • 网站跳转怎么解释关键词查询工具哪个好
  • 网站建设多少钱一个月营销策略包括哪些内容
  • 做粘土网站网站关键词快速排名服务
  • 想自己做网站流程最热门的短期培训课程
  • 做百度推广网站被攻击乔拓云智能建站官网
  • 广东深圳网站建设微信商城运营如何在互联网上做推广
  • 微信网站测试南京疫情最新消息
  • 网站建设首页怎么优化百度关键词
  • 常德网站开发网站运营谷歌seo推广培训班
  • ps4gta5网站建设中小红书seo是什么意思
  • 花卉电子商务网站开发博客网站seo
  • 哪个网站专门做灵异文论坛seo设置
  • 做mla的网站昆山seo网站优化软件
  • 做网站推广哪家公司好武汉seo诊断
  • 招聘网站排行榜品牌营销策划方案怎么做
  • 临沂供电公司网站seo网络推广排名
  • java做网站书厨师培训
  • wordpress商店如何南昌seo技术外包
  • 洛阳网站建设哪家权威潍坊网站建设解决方案
  • 家具企业网站模板seo优化服务商
  • 模板网站与定制网站的价格c盘优化大师
  • 使用php做的网站seochan是什么意思
  • 深圳网站开发公济南seo优化公司助力网站腾飞
  • WordPress图片投稿插件semseo是什么意思
  • 广州网络营销首荐佐兰网络vip网站排名优化价格
  • 兰州最新通知长沙seo免费诊断
  • 嘉兴做微网站的公司国际军事最新消息今天