Django中提示消息messages的設置方式
更新時間:2019年11月15日 10:04:22 作者:查理·布朗
今天小編就為大家分享一篇Django中提示消息messages的設置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
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, '親,搜索內(nèi)容不能為空哦。') return redirect('/') elif c2.exists(): messages.error(request, '對不起,您不能再次借閱該圖書了。') return redirect('/') elif c3 == 5: messages.error(request, '對不起,您將超出圖書館借閱圖書數(shù)量限制。') 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 %}
以上這篇Django中提示消息messages的設置方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
PyTorch的張量tensor和自動求導autograd詳解
這篇文章主要介紹了PyTorch的張量tensor和自動求導autograd,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02極簡Python庫CherryPy構(gòu)建高性能Web應用實例探索
今天為大家介紹的是 CherryPy,它是一個極簡、穩(wěn)定且功能強大的Web框架,可以幫助開發(fā)者快速構(gòu)建高性能的 Web 應用程序,使用 CherryPy,你可以輕松地創(chuàng)建RESTful API、靜態(tài)網(wǎng)站、異步任務和 WebSocket 等應用2024-01-01Python發(fā)送網(wǎng)絡請求(requests)
這篇文章主要介紹了Python發(fā)送網(wǎng)絡請求(requests),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python?基本結(jié)構(gòu)語句(函數(shù)和模塊)
這篇文章主要介紹了python?基本結(jié)構(gòu)語句(函數(shù)和模塊),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09