🍅大家好,今天给大家分享一个Python项目,感兴趣的可以先收藏起来,点赞、关注不迷路!🍅
大家在毕设选题,项目以及论文编写等相关问题都可以给我留言咨询,希望帮助同学们顺利毕业 。1、项目介绍
技术栈:
Python语言、Django框架、Highcharts可视化、MySQL数据库、HTML
本论文所研究的课题是以 Django为基础,设计并实现了一个以 Python为核心的图书管理系统,其中的程序采用了Django中的Web框架,而数据库则采用了 Django中的 SQlite。本系统在设计上简易、直观,方便用户进行使用,除了图书管理方面,还可以对书籍等信息进行数据可视化功能。
2、项目界面
(1)图书数据可视化分析
(2)图书借阅概况
(3)最近事件
(4)借阅记录
(5)图书管理
(6)会员权限管理
(7)注册登录界面
(8)后台数据管理
3、项目说明
随着当前科学技术的发展,人类对于知识的需求日益增大,图书馆就成为人类获取知识的重要场所,而开发一个图书管理系统对图书进行管理的需求随之增大。
本论文所研究的课题是以 Django为基础,设计并实现了一个以 Python为核心的图书管理系统,其中的程序采用了Django中的Web框架,而数据库则采用了 Django中的 SQlite。本系统在设计上简易、直观,方便用户进行使用,除了图书管理方面,还可以对书籍等信息进行数据可视化功能。
基于上述技术,本系统满足实际生活中的图书管理系统的功能需求。本系统主要实现了登录、主页、借书、借阅记录、书籍、书籍类别、出版者、会员、个人中心等功能,主要设计登录模块、菜单模块、借阅模块、图书管理模块、个人中心模块这几大模块。经测试,本系统运行状态良好,基本上满足开发需求。
文章的结尾部分,对全文所做的工作做了一个小结,并提出了今后的工作方向。
关键词: Django;Python;图书管理系统
4、核心代码
# HomePage
class HomeView(LoginRequiredMixin,TemplateView):
login_url = 'login'
template_name = "index.html"
context={}
# users = User.objects.all()
# for user in users:
# print(user.get_username(),user.is_superuser)
def get(self,request, *args, **kwargs):
book_count = Book.objects.aggregate(Sum('quantity'))['quantity__sum']
data_count = {"book":book_count,
"member":Member.objects.all().count(),
"category":Category.objects.all().count(),
"publisher":Publisher.objects.all().count(),}
user_activities= UserActivity.objects.order_by("-created_at")[:5]
user_avatar = { e.created_by:Profile.objects.get(user__username=e.created_by).profile_pic.url for e in user_activities}
short_inventory =Book.objects.order_by('quantity')[:5]
current_week = date.today().isocalendar()[1]
new_members = Member.objects.order_by('-created_at')[:5]
new_members_thisweek = Member.objects.filter(created_at__week=current_week).count()
lent_books_thisweek = BorrowRecord.objects.filter(created_at__week=current_week).count()
books_return_thisweek = BorrowRecord.objects.filter(end_day__week=current_week)
number_books_return_thisweek = books_return_thisweek.count()
new_closed_records = BorrowRecord.objects.filter(open_or_close=1).order_by('-closed_at')[:5]
self.context['data_count']=data_count
self.context['recent_user_activities']=user_activities
self.context['user_avatar']=user_avatar
self.context['short_inventory']=short_inventory
self.context['new_members']=new_members
self.context['new_members_thisweek']=new_members_thisweek
self.context['lent_books_thisweek']=lent_books_thisweek
self.context['books_return_thisweek']=books_return_thisweek
self.context['number_books_return_thisweek']=number_books_return_thisweek
self.context['new_closed_records']=new_closed_records
return render(request, self.template_name, self.context)
# Global Serch
@login_required(login_url='login')
def global_serach(request):
search_value = request.POST.get('global_search')
if search_value =='':
return HttpResponseRedirect("/")
r_category = Category.objects.filter(Q(name__icontains=search_value))
r_publisher = Publisher.objects.filter(Q(name__icontains=search_value)|Q(contact__icontains=search_value))
r_book = Book.objects.filter(Q(author__icontains=search_value)|Q(title__icontains=search_value))
r_member = Member.objects.filter(Q(name__icontains=search_value)|Q(card_number__icontains=search_value)|Q(phone_number__icontains=search_value))
r_borrow = BorrowRecord.objects.filter(Q(borrower__icontains=search_value)|Q(borrower_card__icontains=search_value)|Q(book__icontains=search_value))
context={
'categories':r_category,
'publishers':r_publisher,
'books':r_book,
'members':r_member,
'records':r_borrow,
}
return render(request, 'book/global_search.html',context=context)
# Chart
class ChartView(LoginRequiredMixin,TemplateView):
template_name = "book/charts.html"
login_url = 'login'
context={}
def get(self,request, *args, **kwargs):
top_5_book= Book.objects.order_by('-quantity')[:5].values_list('title','quantity')
top_5_book_titles = [b[0] for b in top_5_book ]
top_5_book__quantities = [b[1] for b in top_5_book ]
# print(top_5_book_titles,top_5_book__quantities)
top_borrow = Book.objects.order_by('-total_borrow_times')[:5].values_list('title','total_borrow_times')
top_borrow_titles = [b[0] for b in top_borrow ]
top_borrow_times = [b[1] for b in top_borrow ]
r_open = BorrowRecord.objects.filter(open_or_close=0).count()
r_close = BorrowRecord.objects.filter(open_or_close=1).count()
m = Member.objects.annotate(month=TruncMonth('created_at')).values('month').annotate(c=Count('id'))
months_member = [e['month'].strftime("%m/%Y") for e in m]
count_monthly_member= [e['c'] for e in m]
self.context['top_5_book_titles']=top_5_book_titles
self.context['top_5_book__quantities']=top_5_book__quantities
self.context['top_borrow_titles']=top_borrow_titles
self.context['top_borrow_times']=top_borrow_times
self.context['r_open']=r_open
self.context['r_close']=r_close
self.context['months_member']=months_member
self.context['count_monthly_member']=count_monthly_member
return render(request, self.template_name, self.context)
# Book
class BookListView(LoginRequiredMixin,ListView):
login_url = 'login'
model=Book
context_object_name = 'books'
template_name = 'book/book_list.html'
search_value=""
order_field="-updated_at"
def get_queryset(self):
search =self.request.GET.get("search")
order_by=self.request.GET.get("orderby")
if order_by:
all_books = Book.objects.all().order_by(order_by)
self.order_field=order_by
else:
all_books = Book.objects.all().order_by(self.order_field)
if search:
all_books = all_books.filter(
Q(title__icontains=search)|Q(author__icontains=search)
)
self.search_value=search
self.count_total = all_books.count()
paginator = Paginator(all_books, PAGINATOR_NUMBER)
page = self.request.GET.get('page')
books = paginator.get_page(page)
return books
def get_context_data(self, *args, **kwargs):
context = super(BookListView, self).get_context_data(*args, **kwargs)
context['count_total'] = self.count_total
context['search'] = self.search_value
context['orderby'] = self.order_field
context['objects'] = self.get_queryset()
return context
class BookDetailView(LoginRequiredMixin,DetailView):
model = Book
context_object_name = 'book'
template_name = 'book/book_detail.html'
login_url = 'login'
comment_form = CommentForm()
# def get_object(self, queryset=None):
# obj = super(BookDetailView, self).get_object(queryset=queryset)
# return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
current_book_name = self.get_object().title
logger.info(f'Book <<{current_book_name}>> retrieved from db')
comments = Comment.objects.filter(book=self.get_object().id)
related_records = BorrowRecord.objects.filter(book=current_book_name)
context['related_records'] = related_records
context['comments'] = comments
context['comment_form'] = self.comment_form
return context
class BookCreateView(LoginRequiredMixin,CreateView):
model=Book
login_url = 'login'
form_class=BookCreateEditForm
template_name='book/book_create.html'
def post(self,request, *args, **kwargs):
super(BookCreateView,self).post(request)
new_book_name = request.POST['title']
messages.success(request, f"New Book << {new_book_name} >> Added")
UserActivity.objects.create(created_by=self.request.user.username,
target_model=self.model.__name__,
detail =f"Create {self.model.__name__} << {new_book_name} >>")
return redirect('book_list')
class BookUpdateView(LoginRequiredMixin,UpdateView):
model = Book
login_url = 'login'
form_class=BookCreateEditForm
template_name = 'book/book_update.html'
def post(self, request, *args, **kwargs):
current_book = self.get_object()
current_book.updated_by=self.request.user.username
current_book.save(update_fields=['updated_by'])
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="warning",
target_model=self.model.__name__,
detail =f"Update {self.model.__name__} << {current_book.title} >>")
return super(BookUpdateView, self).post(request, *args, **kwargs)
def form_valid(self, form):
title=form.cleaned_data['title']
messages.warning(self.request, f"Update << {title} >> success")
return super().form_valid(form)
class BookDeleteView(LoginRequiredMixin,View):
login_url = 'login'
def get(self,request,*args,**kwargs):
book_pk=kwargs["pk"]
delete_book=Book.objects.get(pk=book_pk)
model_name = delete_book.__class__.__name__
messages.error(request, f"Book << {delete_book.title} >> Removed")
delete_book.delete()
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="danger",
target_model=model_name,
detail =f"Delete {model_name} << {delete_book.title} >>")
return HttpResponseRedirect(reverse("book_list"))
# Categorty
class CategoryListView(LoginRequiredMixin,ListView):
login_url = 'login'
model=Category
context_object_name = 'categories'
template_name = 'book/category_list.html'
count_total = 0
search_value = ''
order_field="-created_at"
def get_queryset(self):
search =self.request.GET.get("search")
order_by=self.request.GET.get("orderby")
if order_by:
all_categories = Category.objects.all().order_by(order_by)
self.order_field=order_by
else:
all_categories = Category.objects.all().order_by(self.order_field)
if search:
all_categories = all_categories.filter(
Q(name__icontains=search)
)
self.search_value=search
self.count_total = all_categories.count()
paginator = Paginator(all_categories, PAGINATOR_NUMBER)
page = self.request.GET.get('page')
categories = paginator.get_page(page)
return categories
def get_context_data(self, *args, **kwargs):
context = super(CategoryListView, self).get_context_data(*args, **kwargs)
context['count_total'] = self.count_total
context['search'] = self.search_value
context['orderby'] = self.order_field
context['objects'] = self.get_queryset()
return context
class CategoryCreateView(LoginRequiredMixin,CreateView):
login_url = 'login'
model=Category
fields=['name']
template_name='book/category_create.html'
success_url = reverse_lazy('category_list')
def form_valid(self, form):
new_cat = form.save(commit=False)
new_cat.save()
send_notification(self.request.user,new_cat,verb=f'Add New Category << {new_cat.name} >>')
logger.info(f'{self.request.user} created Category {new_cat.name}')
UserActivity.objects.create(created_by=self.request.user.username,
target_model=self.model.__name__,
detail =f"Create {self.model.__name__} << {new_cat.name} >>")
return super(CategoryCreateView, self).form_valid(form)
class CategoryDeleteView(LoginRequiredMixin,View):
login_url = 'login'
def get(self,request,*args,**kwargs):
cat_pk=kwargs["pk"]
delete_cat=Category.objects.get(pk=cat_pk)
model_name = delete_cat.__class__.__name__
messages.error(request, f"Category << {delete_cat.name} >> Removed")
delete_cat.delete()
send_notification(self.request.user,delete_cat,verb=f'Delete Category << {delete_cat.name} >>')
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="danger",
target_model=model_name,
detail =f"Delete {model_name} << {delete_cat.name} >>")
logger.info(f'{self.request.user} delete Category {delete_cat.name}')
return HttpResponseRedirect(reverse("category_list"))
# Publisher
class PublisherListView(LoginRequiredMixin,ListView):
login_url = 'login'
model=Publisher
context_object_name = 'publishers'
template_name = 'book/publisher_list.html'
count_total = 0
search_value = ''
order_field="-created_at"
def get_queryset(self):
search =self.request.GET.get("search")
order_by=self.request.GET.get("orderby")
if order_by:
all_publishers = Publisher.objects.all().order_by(order_by)
self.order_field=order_by
else:
all_publishers = Publisher.objects.all().order_by(self.order_field)
if search:
all_publishers = all_publishers.filter(
Q(name__icontains=search) | Q(city__icontains=search) | Q(contact__icontains=search)
)
else:
search = ''
self.search_value=search
self.count_total = all_publishers.count()
paginator = Paginator(all_publishers, PAGINATOR_NUMBER)
page = self.request.GET.get('page')
publishers = paginator.get_page(page)
return publishers
def get_context_data(self, *args, **kwargs):
context = super(PublisherListView, self).get_context_data(*args, **kwargs)
context['count_total'] = self.count_total
context['search'] = self.search_value
context['orderby'] = self.order_field
context['objects'] = self.get_queryset()
return context
class PublisherCreateView(LoginRequiredMixin,CreateView):
model=Publisher
login_url = 'login'
form_class=PubCreateEditForm
template_name='book/publisher_create.html'
success_url = reverse_lazy('publisher_list')
def form_valid(self,form):
new_pub = form.save(commit=False)
new_pub.save()
messages.success(self.request, f"New Publisher << {new_pub.name} >> Added")
send_notification(self.request.user,new_pub,verb=f'Add New Publisher << {new_pub.name} >>')
logger.info(f'{self.request.user} created Publisher {new_pub.name}')
UserActivity.objects.create(created_by=self.request.user.username,
target_model=self.model.__name__,
detail =f"Create {self.model.__name__} << {new_pub.name} >>")
return super(PublisherCreateView, self).form_valid(form)
# def post(self,request, *args, **kwargs):
# super(PublisherCreateView,self).post(request)
# new_publisher_name = request.POST['name']
# messages.success(request, f"New Publisher << {new_publisher_name} >> Added")
# UserActivity.objects.create(created_by=self.request.user.username,
# target_model=self.model.__name__,
# detail =f"Create {self.model.__name__} << {new_publisher_name} >>")
# return redirect('publisher_list')
class PublisherUpdateView(LoginRequiredMixin,UpdateView):
model=Publisher
login_url = 'login'
form_class=PubCreateEditForm
template_name = 'book/publisher_update.html'
def post(self, request, *args, **kwargs):
current_pub = self.get_object()
current_pub.updated_by=self.request.user.username
current_pub.save(update_fields=['updated_by'])
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="warning",
target_model=self.model.__name__,
detail =f"Update {self.model.__name__} << {current_pub.name} >>")
return super(PublisherUpdateView, self).post(request, *args, **kwargs)
def form_valid(self, form):
title=form.cleaned_data['name']
messages.warning(self.request, f"Update << {title} >> success")
return super().form_valid(form)
class PublisherDeleteView(LoginRequiredMixin,View):
login_url = 'login'
def get(self,request,*args,**kwargs):
pub_pk=kwargs["pk"]
delete_pub=Publisher.objects.get(pk=pub_pk)
model_name = delete_pub.__class__.__name__
messages.error(request, f"Publisher << {delete_pub.name} >> Removed")
delete_pub.delete()
send_notification(self.request.user,delete_pub,verb=f'Delete Publisher << {delete_pub.name} >>')
logger.info(f'{self.request.user} delete Publisher {delete_pub.name}')
UserActivity.objects.create(created_by=self.request.user.username,
operation_type="danger",
target_model=model_name,
detail =f"Delete {model_name} << {delete_pub.name} >>")
return HttpResponseRedirect(reverse("publisher_list"))
# User Logs
# @method_decorator(user_passes_test(lambda u: u.has_perm("book.view_useractivity")), name='dispatch')
@method_decorator(allowed_groups(group_name=['logs']), name='dispatch')
class ActivityListView(LoginRequiredMixin,ListView):
login_url = 'login'
model= UserActivity
context_object_name = 'activities'
template_name = 'book/user_activity_list.html'
count_total = 0
search_value=''
created_by=''
order_field="-created_at"
all_users = User.objects.values()
user_list = [x['username'] for x in all_users]
# def dispatch(self, *args, **kwargs):
# return super(ActivityListView, self).dispatch(*args, **kwargs)
def get_queryset(self):
data = self.request.GET.copy()
search =self.request.GET.get("search")
filter_user=self.request.GET.get("created_by")
all_activities = UserActivity.objects.all()
if filter_user:
self.created_by = filter_user
all_activities = all_activities.filter(created_by=self.created_by)
if search:
self.search_value = search
all_activities = all_activities.filter(Q(target_model__icontains=search))
self.search_value=search
self.count_total = all_activities.count()
paginator = Paginator(all_activities,PAGINATOR_NUMBER)
page = self.request.GET.get('page')
try:
response = paginator.get_page(page)
except PageNotAnInteger:
response = paginator.get_page(1)
except EmptyPage:
response = paginator.get_page(paginator.num_pages)
return response
def get_context_data(self, *args, **kwargs):
context = super(ActivityListView, self).get_context_data(*args, **kwargs)
context['count_total'] = self.count_total
context['search'] = self.search_value
context['user_list']= self.user_list
context['created_by'] = self.created_by
return context
# @method_decorator(user_passes_test(lambda u: u.has_perm("book.delete_useractivity")), name='dispatch')
@method_decorator(allowed_groups(group_name=['logs']), name='dispatch')
class ActivityDeleteView(LoginRequiredMixin,View):
login_url = 'login'
def get(self,request,*args,**kwargs):
log_pk=kwargs["pk"]
delete_log=UserActivity.objects.get(pk=log_pk)
messages.error(request, f"Activity Removed")
delete_log.delete()
return HttpResponseRedirect(reverse("user_activity_list"))
源码获取:
🍅由于篇幅限制,获取完整文章或源码、代做项目的,查看我的【用户名】、【专栏名称】、【顶部选题链接】就可以找到我啦🍅
感兴趣的可以先收藏起来,点赞、关注不迷路,下方查看👇🏻获取联系方式👇🏻