|
|
# Create your views here.
|
|
|
from django.db.models import Q
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
|
|
from . import models
|
|
|
from .models import Medicine
|
|
|
from .forms import MedicineForm, LoginForm # 假设你已经创建了表单类
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
from django.http import JsonResponse
|
|
|
|
|
|
|
|
|
def medicine_list(request):
|
|
|
medicines = Medicine.objects.all()
|
|
|
return render(request, 'medicine_list.html', {'medicines': medicines})
|
|
|
|
|
|
|
|
|
def medicine_create(request):
|
|
|
if request.method == 'POST':
|
|
|
form = MedicineForm(request.POST)
|
|
|
if form.is_valid():
|
|
|
form.save()
|
|
|
return redirect('/medicine/list/')
|
|
|
else:
|
|
|
form = MedicineForm()
|
|
|
|
|
|
return render(request, 'medicine_create.html', {'form': form})
|
|
|
|
|
|
|
|
|
# 修改药品
|
|
|
def medicine_edit(request, pk):
|
|
|
# medicine = get_object_or_404(Medicine, pk=pk)
|
|
|
medicine = models.Medicine.objects.filter(id=pk).first()
|
|
|
if request.method == 'GET':
|
|
|
form = MedicineForm(instance=medicine)
|
|
|
return render(request, 'medicine_edit.html', {'form': form})
|
|
|
|
|
|
form = MedicineForm(request.POST, instance=medicine)
|
|
|
print(form.is_valid())
|
|
|
if form.is_valid():
|
|
|
form.save()
|
|
|
return redirect('/medicine/list')
|
|
|
return render(request, 'medicine_edit.html', {'form': form})
|
|
|
|
|
|
|
|
|
# 删除药品
|
|
|
def medicine_delete(request, pk):
|
|
|
# medicine = get_object_or_404(Medicine, pk=pk)
|
|
|
# if request.method == 'POST':
|
|
|
# medicine.delete()
|
|
|
# return redirect('/medicine/list/')
|
|
|
# return render(request, 'medicine_confirm_delete.html', {'medicine': medicine})
|
|
|
# 获取id
|
|
|
# 删除
|
|
|
models.Medicine.objects.filter(id=pk).delete()
|
|
|
# 重定向列表
|
|
|
return redirect("/medicine/list/")
|
|
|
|
|
|
|
|
|
def medicine_search(request):
|
|
|
query = request.GET.get('query')
|
|
|
if query:
|
|
|
# 使用Q对象进行模糊搜索
|
|
|
medicines = Medicine.objects.filter(Q(name__icontains=query) | Q(description__icontains=query))
|
|
|
else:
|
|
|
medicines = Medicine.objects.all()
|
|
|
return render(request, 'medicine_list.html', {'medicines': medicines})
|
|
|
|
|
|
|
|
|
def login(request):
|
|
|
""" 登录 """
|
|
|
if request.method == "GET":
|
|
|
form = LoginForm()
|
|
|
return render(request, 'login.html', {'form': form})
|
|
|
|
|
|
form = LoginForm(data=request.POST)
|
|
|
if form.is_valid():
|
|
|
# 验证成功,获取到的用户名和密码
|
|
|
# {'username': 'wupeiqi', 'password': '123',"code":123}
|
|
|
# {'username': 'wupeiqi', 'password': '5e5c3bad7eb35cba3638e145c830c35f',"code":xxx}
|
|
|
|
|
|
# 去数据库校验用户名和密码是否正确,获取用户对象、None
|
|
|
# admin_object = models.Admin.objects.filter(username=xxx, password=xxx).first()
|
|
|
admin_object = models.Admin.objects.filter(**form.cleaned_data).first()
|
|
|
# print(admin_object)
|
|
|
if not admin_object:
|
|
|
form.add_error("password", "用户名或密码错误")
|
|
|
# form.add_error("username", "用户名或密码错误")
|
|
|
return render(request, 'login.html', {'form': form})
|
|
|
|
|
|
request.session["info"] = {'id': admin_object.id, 'name': admin_object.username}
|
|
|
# session可以保存7天
|
|
|
request.session.set_expiry(60 * 60 * 24 * 7)
|
|
|
|
|
|
return redirect("/medicine/list")
|
|
|
|
|
|
return render(request, 'login.html', {'form': form})
|
|
|
|
|
|
|
|
|
def logout(request):
|
|
|
""" 注销 """
|
|
|
|
|
|
request.session.clear()
|
|
|
|
|
|
return redirect('/login/')
|