使用 messages 闪现
在views.py中导入
from django.contrib import messages
在html中
{% if messages %}
{% for mess in messages %}
<font size="5" color="red">{{mess}}</font> <br><br>
{% endfor %}
{% endif %}
django中的增删改查
删
def dele(request):
id = request.GET.get('id')
dele = models.Department.objects.filter(id=id).delete()
return redirect('index')
改
def amend(request):
id = request.GET.get('id')
course = models.Department.objects.get(id=id)
if request.method == 'POST':
department_name = request.POST.get('department_name')
department_work = request.POST.get('department_work')
course.save()
return redirect('index')
return render(request,'web/department_add.html',locals())
在首页显示name
def index(request):
if request.method == 'GET':
account = request.session.get('account')
user= models.Teacher.objects.filter(account=account).first()
if not account :
return redirect(reverse('login')) #在首页展示登录人的名字
return render(request,'web/index.html',locals())
在首页显示账号
def index(request):
if request.method == 'POST':
name = request.POST.get('name')
date = models.Staff.objects.filter(name=name).all()
return render(request,'web/index.html',locals())
退出该用户
def delete(request): # 退出
dele = request.session.get('account')
if dele:
del request.session['account']
return redirect(reverse('index'))
查
def index(request): #首页
if request.method == 'POST':
datas = models.Department.objects.all()
date = models.Staff.objects.all()
return render(request,'web/index.html',locals()) #搜索