Django中提示消息messages的设置方式

作者:查理·布朗 时间:2021-08-14 20:40:51 

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 %}

来源:https://www.cnblogs.com/zihao1037/p/11037801.html

标签:Django,提示消息,messages
0
投稿

猜你喜欢

  • Python导入不同文件夹中文件的方法详解

    2022-01-22 09:30:08
  • 如何使用AspUpload2.0组件?

    2010-06-05 12:33:00
  • 网页对联广告代码效果大全

    2007-10-25 23:16:00
  • thinkphp3.x连接mysql数据库的方法(具体操作步骤)

    2023-11-22 20:04:41
  • 表单验证中时间起止判断的递归处理

    2009-12-16 19:27:00
  • File input 的样式和文字的更改方法__适用于Firefox、IE等浏览器

    2009-10-13 21:05:00
  • php桥接模式的实例用法及代码分析

    2023-11-18 15:46:33
  • Python中的元类编程入门指引

    2023-08-02 02:43:44
  • 再谈CSS样式表书写风格

    2009-03-30 16:09:00
  • 设计需知:移动网站设计应注意的技术细节

    2012-04-20 13:03:23
  • Python的matplotlib绘图如何修改背景颜色的实现

    2023-10-05 08:12:43
  • PHP session会话的安全性分析

    2023-11-21 23:47:59
  • 多栏自适应布局问题浅谈

    2010-08-16 12:56:00
  • 建立三层结构的ASP应用程序

    2009-01-21 19:41:00
  • Thinkphp5.0 框架的请求方式与响应方式分析

    2023-11-15 00:07:09
  • Python中多线程及程序锁浅析

    2023-06-02 02:59:33
  • Python简单遍历字典及删除元素的方法

    2021-12-31 08:57:51
  • 利用python绘制正态分布曲线

    2021-04-09 06:59:40
  • ASP与Excel结合生成数据表和Chart图的代码

    2011-03-08 10:50:00
  • Python装饰器实现方法及应用场景详解

    2022-04-30 22:57:49
  • asp之家 网络编程 m.aspxhome.com