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的设置方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com