提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
一、Django是什么?
二、django安装
1.安装
2.需要的pip list 安装
三、django 建立图书馆管理系统
1.建立项目
建立项目locallibrary
2.参加APP
建立数据库sqllite的数据库
建立用user的createsuper 超级用户
图书后台管理系统
新建作者
新建图书
新建类型
新建语言
编辑借书
图书管理系统页面
编辑
作者列表
已借图书列表编辑
我借的图书列表编辑
图书管理系统编辑
总结
前言
Django 快速建立图书借书系统
提示:以下是本篇文章正文内容,下面案例可供参考
一、Django是什么?
Django是高水准的Python编程语言驱动的一个开源模型.视图,控制器风格的Web应用程序框架,它起源于开源社区。使用这种架构,程序员可以方便、快捷地创建高品质、易维护、数据库驱动的应用程序。这也正是OpenStack的Horizon组件采用这种架构进行设计的主要原因。另外,在Dj ango框架中,还包含许多功能强大的第三方插件,使得Django具有较强的可扩展性 。Django 项目源自一个在线新闻 Web 站点。Django 框架的核心组件有:
-
用于创建模型的对象关系映射;
-
为最终用户设计较好的管理界面;
-
URL 设计;
-
设计者友好的模板语言;
-
缓存系统。
二、django安装
1.安装
代码如下(示例):
pip install django
2.需要的pip list 安装
代码如下(示例):
dj-database-url==0.5.0
Django==4.0.2
gunicorn==20.1.0
psycopg2-binary==2.9.3
wheel==0.37.1
whitenoise==6.0.0
三、django 建立图书馆管理系统
1.建立项目
代码如下(示例):
django-admin startproject locallibrary
建立项目locallibrary
2.参加APP
python manage.py startapp catalog
urls.py 是后来进行创建。
先建立models.py文件
from django.db import models
# Create your models here.
from django.urls import reverse # To generate URLS by reversing URL patterns
class Genre(models.Model):
"""Model representing a book genre (e.g. Science Fiction, Non Fiction)."""
name = models.CharField(
max_length=200,
help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)"
)
def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name
class Language(models.Model):
"""Model representing a Language (e.g. English, French, Japanese, etc.)"""
name = models.CharField(max_length=200,
help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)")
def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name
class Book(models.Model):
"""Model representing a book (but not a specific copy of a book)."""
title = models.CharField(max_length=200)
author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
# Foreign Key used because book can only have one author, but authors can have multiple books
# Author as a string rather than object because it hasn't been declared yet in file.
summary = models.TextField(max_length=1000, help_text="Enter a brief description of the book")
isbn = models.CharField('ISBN', max_length=13,
unique=True,
help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn'
'">ISBN number</a>')
genre = models.ManyToManyField(Genre, help_text="Select a genre for this book")
# ManyToManyField used because a genre can contain many books and a Book can cover many genres.
# Genre class has already been defined so we can specify the object above.
language = models.ForeignKey('Language', on_delete=models.SET_NULL, null=True)
class Meta:
ordering = ['title', 'author']
def display_genre(self):
"""Creates a string for the Genre. This is required to display genre in Admin."""
return ', '.join([genre.name for genre in self.genre.all()[:3]])
display_genre.short_description = 'Genre'
def get_absolute_url(self):
"""Returns the url to access a particular book instance."""
return reverse('book-detail', args=[str(self.id)])
def __str__(self):
"""String for representing the Model object."""
return self.title
import uuid # Required for unique book instances
from datetime import date
from django.contrib.auth.models import User # Required to assign User as a borrower
class BookInstance(models.Model):
"""Model representing a specific copy of a book (i.e. that can be borrowed from the library)."""
id = models.UUIDField(primary_key=True, default=uuid.uuid4,
help_text="Unique ID for this particular book across whole library")
book = models.ForeignKey('Book', on_delete=models.RESTRICT, null=True)
imprint = models.CharField(max_length=200)
due_back = models.DateField(null=True, blank=True)
borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
@property
def is_overdue(self):
"""Determines if the book is overdue based on due date and current date."""
return bool(self.due_back and date.today() > self.due_back)
LOAN_STATUS = (
('d', 'Maintenance'),
('o', 'On loan'),
('a', 'Available'),
('r', 'Reserved'),
)
status = models.CharField(
max_length=1,
choices=LOAN_STATUS,
blank=True,
default='d',
help_text='Book availability')
class Meta:
ordering = ['due_back']
permissions = (("can_mark_returned", "Set book as returned"),)
def __str__(self):
"""String for representing the Model object."""
return '{0} ({1})'.format(self.id, self.book.title)
class Author(models.Model):
"""Model representing an author."""
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField(null=True, blank=True)
date_of_death = models.DateField('died', null=True, blank=True)
class Meta:
ordering = ['last_name', 'first_name']
def get_absolute_url(self):
"""Returns the url to access a particular author instance."""
return reverse('author-detail', args=[str(self.id)])
def __str__(self):
"""String for representing the Model object."""
return '{0}, {1}'.format(self.last_name, self.first_name)
建立数据库sqllite的数据库
python3 manage.py makemigrations
python3 manage.py migrate
建立用user的createsuper 超级用户
图书后台管理系统
作者类、图书、图书借、类型、图书语言
进入Admin的输入必要数据
新建作者
新建图书
新建类型
新建语言
借书
后台建立5个基本数据,目前建立一个1本书
图书管理系统页面
作者列表
已借图书列表
我借的图书列表
图书管理系统
总结
图书管理系统,用django 快速生成的增加修改删除,数据库也是内置的数据库