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

yipeiwu_com5年前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 %}

以上这篇Django中提示消息messages的设置方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python调用graphviz绘制结构化图形网络示例

Python调用graphviz绘制结构化图形网络示例

首先要下载:Graphviz - Graph Visualization Software 安装完成后将安装目录的bin 路径加到系统路径中,有时候需要重启电脑。 然后: pip...

Python基于回溯法子集树模板解决数字组合问题实例

Python基于回溯法子集树模板解决数字组合问题实例

本文实例讲述了Python基于回溯法子集树模板解决数字组合问题。分享给大家供大家参考,具体如下: 问题 找出从自然数1、2、3、...、n中任取r个数的所有组合。 例如,n=5,r=3的...

python实现的系统实用log类实例

本文实例讲述了python实现的系统实用log类。分享给大家供大家参考。具体如下: 每个系统都必不可少会需要一个log类,方便了解系统的运行状况和排错,python本身已经提供了一个lo...

python中os模块详解

os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作。比如说:绝对路径,父目录…… os.sep可以取代操作系统特定的路径分隔符。windows下为...

Python中处理字符串之isalpha()方法的使用

 isalpha()方法检查字符串是否仅由字母组成。 语法 以下是islpha()方法的语法: str.isalpha() 参数   &nb...