基于Python+Django实现药品管理系统
Posted 编程指南针
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Python+Django实现药品管理系统相关的知识,希望对你有一定的参考价值。
项目编号:BS-Python-001
技术架构:Python+Django
前端:Boostrap
开发工具:IDEA
数据库:sqlite
项目实现的功能:
用户分三个角色:管理员、仓库管理员、经销商管理员
主要实现的功能包含:药品的进销存操作、客户管理、供应商管理、经营数据统计、数据导出、系统管理等模块
具体功能展示如下:
库存管理
客户管理
供应商管理
经营统计分析
数据导出
系统管理
数据打印
部分核心代码:
from datetime import datetime
import time
from django.http.response import FileResponse
from django.shortcuts import render, redirect
from hnyhms_app.models import *
import pandas as pd
# 首页
def index(request):
return render(request, 'index.html', context='flag': True)
# 登录
def login(request):
# 用户登录信息判断
if request.method == "POST":
# 获取用户登录信息
username = request.POST.get('username')
userpass = request.POST.get('userpass')
# 查询用户信息
admin_user = Admin_users.objects.filter(au_account=username, au_pwd=userpass).first()
if admin_user:
rep = redirect('/adminpage')
rep.set_cookie('adminuser', username)
rep.set_cookie('au_role', admin_user.au_role)
return rep
rep = render(request, 'index.html', context='flag': False)
rep.delete_cookie("adminuser")
return rep
# 退出登录
def outlogin(request):
rep = redirect('/')
rep.delete_cookie("adminuser")
return rep
# 管理页面
def admin(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
if adminuser is not None:
# 登录用户信息
admin_user = Admin_users.objects.get(au_account=adminuser)
admin_user_name = admin_user.au_name
admin_user_role = admin_user.au_role
if admin_user_role == 1:
return render(request, 'admin_1.html', context='admin_user_name': admin_user_name)
if admin_user_role == 2:
return render(request, 'admin_2.html', context='admin_user_name': admin_user_name)
if admin_user_role == 3:
return render(request, 'admin_3.html', context='admin_user_name': admin_user_name)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 库存管理
def kcpg(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
# 判断是否有查询信息
if request.GET.get('s') is None or request.GET.get('s') == '':
# 返回全部列表
medicines_list = Medicines.objects.all().values()
customers_suppliers = Customers_suppliers.objects.filter(cs_attitude='供应商').values()
warehouse_record = Warehouse_record.objects.all()
context = 'medicines': medicines_list, 'customers_suppliers': customers_suppliers, 'warehouse_record': warehouse_record
return render(request, 'funcpage/kcpg.html', context=context)
# 返回符合查询内容的列表
medicines_list = Medicines.objects.filter(medicine_name__contains=request.GET.get('s')).values()
customers_suppliers = Customers_suppliers.objects.filter(cs_attitude='供应商').values()
warehouse_record = Warehouse_record.objects.all().values()
context = 'medicines': medicines_list, 'customers_suppliers': customers_suppliers, 'warehouse_record': warehouse_record
return render(request, 'funcpage/kcpg.html', context=context)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品入库页面
def in_medicine(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine_id = request.GET.get('id')
medicine_data = list(Medicines.objects.filter(id=medicine_id).values())[0]
customers_suppliers = Customers_suppliers.objects.filter(cs_attitude='供应商').values()
return render(request, 'funcpage/in_medicine.html', context='medicine_data': medicine_data, 'customers_suppliers': customers_suppliers)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品出库
def out_medicine(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine_id = request.GET.get('id')
medicine_data = list(Medicines.objects.filter(id=medicine_id).values())[0]
customers_suppliers = Customers_suppliers.objects.filter(cs_attitude='客户').values()
return render(request, 'funcpage/out_medicine.html', context='medicine_data': medicine_data, 'customers_suppliers': customers_suppliers)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品入库保存
def in_medicine_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine = Medicines.objects.filter(id=request.POST.get('id'))
medicine.update(medicine_quantity = medicine[0].medicine_quantity + float(request.POST.get('medicine_quantity')))
# 进货金额计算
in_amount = float(request.POST.get('medicine_quantity')) * float(request.POST.get('medicine_purchase'))
# 添加入库记录
warehouse_record = Warehouse_record(wr_in_out = '入库',
wr_quantity = request.POST.get('medicine_quantity'),
wr_cs_name = request.POST.get('medicine_suppliers'),
wr_admin_name = request.COOKIES.get('adminuser'),
wr_medicine_name = request.POST.get('medicine_name'),
wr_in_out_time = time.strftime("%Y-%m-%d", time.localtime()),
wr_amount = in_amount)
warehouse_record.save()
return redirect('/kcpg')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品出库保存
def out_medicine_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine = Medicines.objects.filter(id=request.POST.get('id'))
if medicine[0].medicine_quantity >= float(request.POST.get('medicine_quantity')):
medicine.update(medicine_quantity = medicine[0].medicine_quantity - float(request.POST.get('medicine_quantity')))
# 出货金额计算
out_amount = float(request.POST.get('medicine_quantity')) * float(request.POST.get('medicine_selling'))
# 添加出库记录
warehouse_record = Warehouse_record(wr_in_out = '出库',
wr_quantity = request.POST.get('medicine_quantity'),
wr_cs_name = request.POST.get('medicine_suppliers'),
wr_admin_name = request.COOKIES.get('adminuser'),
wr_medicine_name = request.POST.get('medicine_name'),
wr_in_out_time = time.strftime("%Y-%m-%d", time.localtime()),
wr_amount = out_amount)
warehouse_record.save()
return redirect('/kcpg')
else:
return render(request, 'funcpage/error.html', context='info': '库存数量已不够出库,请返回或重新填写出库数量', 'up': '/kcpg', status=403)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 新增药品保存
def add_medicine(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
# 药品保存
medicine = Medicines(medicine_name = request.POST.get('medicine_name'),
medicine_manufacturers = request.POST.get('medicine_manufacturers'),
medicine_trademark = request.POST.get('medicine_trademark'),
medicine_production_address = request.POST.get('medicine_production_address'),
medicine_code = request.POST.get('medicine_code'),
medicine_specification = request.POST.get('medicine_specification'),
medicine_purchase = request.POST.get('medicine_purchase'),
medicine_selling = request.POST.get('medicine_selling'))
medicine.save()
return redirect("/kcpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品编辑页面
def edit_medicine_page(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine_id = request.GET.get('id')
medicine_data = list(Medicines.objects.filter(id=medicine_id).values())[0]
return render(request, 'funcpage/edit_medicine.html', context='medicine_data': medicine_data)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 药品编辑保存
def edit_medicine_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine = Medicines.objects.filter(id=request.POST.get('id'))
medicine.update(medicine_name=request.POST.get('medicine_name'),
medicine_manufacturers = request.POST.get('medicine_manufacturers'),
medicine_trademark = request.POST.get('medicine_trademark'),
medicine_production_address = request.POST.get('medicine_production_address'),
medicine_code = request.POST.get('medicine_code'),
medicine_specification = request.POST.get('medicine_specification'),
medicine_purchase = request.POST.get('medicine_purchase'),
medicine_selling = request.POST.get('medicine_selling'))
return redirect('/kcpg')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 删除库存
def del_medicine(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 2):
medicine = Medicines.objects.get(id = request.GET.get('id'))
medicine.delete()
return redirect("/kcpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 客户管理
def khpg(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
# 判断是否有查询信息
if request.GET.get('s') is None or request.GET.get('s') == '':
customers_suppliers = Customers_suppliers.objects.filter(
cs_attitude='客户').values()
context = 'customers_suppliers': customers_suppliers
return render(request, 'funcpage/khpg.html', context=context)
# 返回符合查询内容的列表
customers_suppliers = Customers_suppliers.objects.filter(cs_unit__contains = request.GET.get('s')).values()
context = 'customers_suppliers': customers_suppliers
return render(request, 'funcpage/khpg.html', context=context)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 新增客户
def add_customers(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
customers = Customers_suppliers(cs_attitude = request.POST.get('cs_attitude'),
cs_postCode = request.POST.get('cs_postCode'),
cs_address = request.POST.get('cs_address'),
cs_tel = request.POST.get('cs_tel'),
cs_unit = request.POST.get('cs_unit'),
cs_name = request.POST.get('cs_name'))
customers.save()
return redirect("/khpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 客户编辑页面
def edit_customers_page(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
customers_id = request.GET.get('id')
customers_data = list(Customers_suppliers.objects.filter(id=customers_id).values())[0]
return render(request, 'funcpage/edit_customers.html', context=customers_data)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 客户编辑保存
def edit_customers_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
medicine = Customers_suppliers.objects.filter(id=request.POST.get('id'))
medicine.update(cs_postCode=request.POST.get('cs_postCode'),
cs_address = request.POST.get('cs_address'),
cs_tel = request.POST.get('cs_tel'),
cs_unit = request.POST.get('cs_unit'),
cs_name = request.POST.get('cs_name'))
return redirect('/khpg')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 删除客户
def del_customers(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
customers_suppliers = Customers_suppliers.objects.get(id = request.GET.get('id'))
customers_suppliers.delete()
return redirect("/khpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 供应商管理
def gyspg(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
# 判断是否有查询信息
if request.GET.get('s') is None or request.GET.get('s') == '':
customers_suppliers = Customers_suppliers.objects.filter(
cs_attitude='供应商').values()
context = 'customers_suppliers': customers_suppliers
return render(request, 'funcpage/gyspg.html', context=context)
# 返回符合查询内容的列表
customers_suppliers = Customers_suppliers.objects.filter(cs_unit__contains = request.GET.get('s')).values()
context = 'customers_suppliers': customers_suppliers
return render(request, 'funcpage/gyspg.html', context=context)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 新增供应商
def add_suppliers(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
suppliers = Customers_suppliers(cs_attitude = request.POST.get('cs_attitude'),
cs_postCode = request.POST.get('cs_postCode'),
cs_address = request.POST.get('cs_address'),
cs_tel = request.POST.get('cs_tel'),
cs_unit = request.POST.get('cs_unit'),
cs_name = request.POST.get('cs_name'))
suppliers.save()
return redirect("/gyspg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 供应商编辑页面
def edit_suppliers_page(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
suppliers_id = request.GET.get('id')
suppliers_data = list(Customers_suppliers.objects.filter(id=suppliers_id).values())[0]
return render(request, 'funcpage/edit_suppliers.html', context=suppliers_data)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 供应商编辑保存
def edit_suppliers_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
suppliers = Customers_suppliers.objects.filter(id=request.POST.get('id'))
suppliers.update(cs_postCode=request.POST.get('cs_postCode'),
cs_address = request.POST.get('cs_address'),
cs_tel = request.POST.get('cs_tel'),
cs_unit = request.POST.get('cs_unit'),
cs_name = request.POST.get('cs_name'))
return redirect('/gyspg')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 删除供应商
def del_suppliers(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1 or admin_user_role == 3):
customers_suppliers = Customers_suppliers.objects.get(id = request.GET.get('id'))
customers_suppliers.delete()
return redirect("/gyspg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 统计分析
def tjpg(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
try:
# 年销售额/成本/占比
# 年数据
year_data =
now_year = datetime.now().year
year_in_warehouse_record = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '入库', wr_in_out_time__year = now_year).values())
year_out_warehouse_record = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '出库', wr_in_out_time__year = now_year).values())
# 年收入
year_data['year_income'] = sum(year_out_warehouse_record['wr_amount'])
# 年支出
year_data['year_expenditure'] = sum(year_in_warehouse_record['wr_amount'])
# 年利润
year_data['year_profit'] = year_data['year_income'] - year_data['year_expenditure']
# 年盈亏比
year_data['year_ratio'] = round((year_data['year_profit'] / year_data['year_expenditure']) * 100, 3)
# 月销售额/成本/占比
# 月数据
month_data =
now_month = datetime.now().month
month_in_warehouse_record = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '入库', wr_in_out_time__month = now_month).values())
month_out_warehouse_record = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '出库', wr_in_out_time__month = now_month).values())
# 月收入
month_data['month_income'] = sum(month_out_warehouse_record['wr_amount'])
# 月支出
month_data['month_expenditure'] = sum(month_in_warehouse_record['wr_amount'])
# 月利润
month_data['month_profit'] = month_data['month_income'] - month_data['month_expenditure']
# 月盈亏比
month_data['month_ratio'] = round((month_data['month_profit'] / month_data['month_expenditure']) * 100, 3)
# 药品排行
medicine_ranking =
# 年药品排行
year_medicine = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '出库', wr_in_out_time__year = now_year).values())
year_medicine = year_medicine.sort_values('wr_amount')
year_medicine_ranking = year_medicine.groupby(['wr_medicine_name'])['wr_amount'].sum()[::-1]
medicine_ranking['year_medicine_ranking'] = year_medicine_ranking.to_dict()
# 月药品排行
month_medicine = pd.DataFrame(Warehouse_record.objects.filter(wr_in_out = '出库', wr_in_out_time__month = now_month).values())
month_medicine = month_medicine.sort_values('wr_amount')
month_medicine_ranking = month_medicine.groupby(['wr_medicine_name'])['wr_amount'].sum()[::-1]
medicine_ranking['month_medicine_ranking'] = month_medicine_ranking.to_dict()
# 盈利亏损情况
pl =
pl['year'] = True
pl['month'] = True
if year_data['year_profit'] < 0:
pl['year'] = False
if month_data['month_profit'] < 0:
pl['month'] = False
context = 'year_data': year_data, 'month_data': month_data, 'medicine_ranking': medicine_ranking, 'pl': pl
return render(request, 'funcpage/tjpg.html', context=context)
except:
return render(request, 'funcpage/error.html', context='info': '现有出入库数据无法进行统计分析!', 'up': '')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 库存数据下载
def down_kc(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
# 生成数据文件
data = pd.DataFrame(Medicines.objects.all().values())
data.to_excel('hnyhms_app/data_file/kc_data.xls', index=False)
# 返回文件
file = open('hnyhms_app/data_file/kc_data.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="kc_data_' + datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S') + '.xls"'
return response
return render(request, 'funcpage/error.html', context='info': '您没有权限下载文件!', 'up': '/')
# 出入库记录下载
def down_io(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
# 生成数据文件
data = pd.DataFrame(Warehouse_record.objects.all().values())
data.to_excel('hnyhms_app/data_file/io_data.xls', index=False)
# 返回文件
file = open('hnyhms_app/data_file/io_data.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="io_data_' + datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S') + '.xls"'
return response
return render(request, 'funcpage/error.html', context='info': '您没有权限下载文件!', 'up': '/')
# 客户数据下载
def down_kh(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
# 生成数据文件
data = pd.DataFrame(Customers_suppliers.objects.filter(cs_attitude='客户').values())
data.to_excel('hnyhms_app/data_file/cs_data.xls', index=False)
# 返回文件
file = open('hnyhms_app/data_file/cs_data.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="cs_data_' + datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S') + '.xls"'
return response
return render(request, 'funcpage/error.html', context='info': '您没有权限下载文件!', 'up': '/')
# 供应商数据下载
def down_gys(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
# 生成数据文件
data = pd.DataFrame(Customers_suppliers.objects.filter(cs_attitude='供应商').values())
data.to_excel('hnyhms_app/data_file/gys_data.xls', index=False)
# 返回文件
file = open('hnyhms_app/data_file/gys_data.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="gys_data_' + datetime.strftime(datetime.now(),'%Y-%m-%d %H:%M:%S') + '.xls"'
return response
return render(request, 'funcpage/error.html', context='info': '您没有权限下载文件!', 'up': '/')
# 系统管理
def xtpg(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
admin_users = Admin_users.objects.all().values()
context = 'admin_users': admin_users
return render(request, 'funcpage/xtpg.html', context=context)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 新增管理员
def add_admin_users(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
admin_users = Admin_users(au_name = request.POST.get('au_name'),
au_account = request.POST.get('au_account'),
au_pwd = request.POST.get('au_pwd'),
au_role = request.POST.get('au_role'),
au_tel = request.POST.get('au_tel'))
admin_users.save()
return redirect("/xtpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 管理员编辑页面
def edit_admin_page(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
admin_id = request.GET.get('id')
admin_data = list(Admin_users.objects.filter(id=admin_id).values())[0]
return render(request, 'funcpage/edit_admin.html', context=admin_data)
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 管理员编辑保存
def edit_admin_save(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
admin = Admin_users.objects.filter(id=request.POST.get('id'))
if request.POST.get('au_pwd') == '':
admin.update(au_name=request.POST.get('au_name'),
au_account = request.POST.get('au_account'),
au_role = request.POST.get('au_role'),
au_tel = request.POST.get('au_tel'))
else:
admin.update(au_name=request.POST.get('au_name'),
au_account = request.POST.get('au_account'),
au_pwd = request.POST.get('au_pwd'),
au_role = request.POST.get('au_role'),
au_tel = request.POST.get('au_tel'))
return redirect('/xtpg')
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
# 删除管理员
def del_adminuser(request):
# 收到浏览器的再次请求,判断浏览器携带的cookie是不是登录成功的时候响应的cookie
adminuser = request.COOKIES.get('adminuser')
# 权限判断
admin_user_role = Admin_users.objects.get(au_account = adminuser).au_role
if adminuser is not None and (admin_user_role == 1):
admin_users = Admin_users.objects.get(id = request.GET.get('id'))
admin_users.delete()
return redirect("/xtpg")
return render(request, 'funcpage/error.html', context='info': '您没有权限访问该页面!', 'up': '/', status=403)
from django.db import models
# Create your models here.
# 管理员
class Admin_users(models.Model):
au_name = models.CharField('姓名', max_length=30)
au_account = models.CharField('账号', max_length=30)
au_pwd = models.CharField('密码', max_length=30)
au_role = models.IntegerField('角色', default=2)
au_tel = models.CharField('电话', max_length=30)
def __str__(self):
return self.au_name
class Meta:
verbose_name = '管理员'
verbose_name_plural = '管理员'
# 供应商和客户
class Customers_suppliers(models.Model):
cs_attitude = models.CharField('供应商/客户', max_length=20)
cs_postCode = models.IntegerField('邮编', default=0)
cs_address = models.CharField('地址', max_length=50)
cs_tel = models.CharField('电话', max_length=20)
cs_unit = models.CharField('单位', max_length=30) # 单位
cs_name = models.CharField('联系人', max_length=30) # 联系人
def __str__(self):
return self.cs_unit
class Meta:
verbose_name = '供应商和客户'
verbose_name_plural = '供应商和客户'
# 药品
class Medicines(models.Model):
medicine_name = models.CharField('药品名', max_length=30)
medicine_manufacturers = models.CharField('生产商', max_length=30) # 生产商
medicine_trademark = models.CharField('品牌', max_length=30) # 品牌
medicine_production_address = models.CharField('生产地址', max_length=50) # 生产地址
medicine_code = models.CharField('条码号', max_length=30)
medicine_specification = models.CharField('规格', max_length=20) # 规格
medicine_purchase = models.FloatField('进货价', default=0) # 进货价
medicine_selling = models.FloatField('出售价', default=0) # 出售价
medicine_quantity = models.IntegerField('数量', default=0) # 出售价
def __str__(self):
return self.medicine_name
class Meta:
verbose_name = '药品'
verbose_name_plural = '药品'
# 出入库
class Warehouse_record(models.Model):
wr_in_out = models.CharField('入库/出库', max_length=20)
wr_quantity = models.IntegerField('数量', default=0) # 出入库量
wr_cs_name = models.CharField('供应商/客户', max_length=20)
wr_admin_name = models.CharField('记录管理员', max_length=20)
wr_medicine_name = models.CharField('药品名', max_length=20)
wr_in_out_time = models.DateField('时间', auto_now=True)
wr_amount = models.FloatField('金额', default=0)
class Meta:
verbose_name = '出入库记录'
verbose_name_plural = '出入库记录'
以上是关于基于Python+Django实现药品管理系统的主要内容,如果未能解决你的问题,请参考以下文章
基于WinCE和SQLite的微量药品称重系统终端设计及实现