时间:2020-10-11 10:47:45 | 栏目:Python代码 | 点击:次
1. 引入messages模块
from django.contrib import messages
2. 把messages写入view中
@csrf_exempt def search(request): if request.method == 'GET': bookname = request.GET.get('bookname') bookinfo = Book.objects.filter(bookname=bookname) is_staff = request.user.is_staff username = request.user.username gonggao = Gonggao.objects.all().order_by("-c_time") liuyanban_all = liuyan.objects.all().order_by("-c_time") c2 = JieInfo.objects.filter(u_name=username, book_name=bookname) c3 = JieInfo.objects.filter(u_name=username).count() if username == '': messages.error(request, '亲,请您先登录,才能使用该功能。') return redirect('/') else: if bookname == '': messages.error(request, '亲,搜索内容不能为空哦。') return redirect('/') elif c2.exists(): messages.error(request, '对不起,您不能再次借阅该图书了。') return redirect('/') elif c3 == 5: messages.error(request, '对不起,您将超出图书馆借阅图书数量限制。') return redirect('/') else: if bookinfo.exists(): return render(request, "systeam/searched.html", {'bookinfo': bookinfo, 'username': username, 'is_staff': is_staff}) else: messages.error(request, '亲,没有这本书呢。') return redirect('/') else: return render(request, "systeam/searched.html", {'username': username})
3. 把messages渲染到页面中
{% if messages %} <ul class="messages"> {% for message in messages %} <li{%if message.tags %} class="{{ message.tags }}"{% endif %}> <div class="m_title"> {{ message.tags }} <a href="" id=" rel="external nofollow" rel="external nofollow" a_tuichu"> <i class="iconfont icon-fork"></i> </a> </div> <div id="m_box"> {{ message }} </div> <a href="" id=" rel="external nofollow" rel="external nofollow" a_sure"> <button id="button_sure"> 确认 </button> </a> </li> {% endfor %} </ul> {% endif %}