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多线程编程(七):使用Condition实现复杂同步

目前我们已经会使用Lock去对公共资源进行互斥访问了,也探讨了同一线程可以使用RLock去重入锁,但是尽管如此我们只不过才处理了一些程序中简单的同步现象,我们甚至还不能很合理的去解决使用...

django settings.py 配置文件及介绍

django settings.py 配置文件及介绍

django settings.py 配置文件 import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspat...

Python中的集合介绍

Python中的集合介绍

1.集合的定义 集合的元素是不可重复的 s = {1,2,3,1,2,3,4,5} print(s) print(type(s)) s1 = {1} print(s1) print(...

Python补齐字符串长度的实例

如下所示: ljust(len,str)字符向左对齐,用str补齐长度 rjust(len,str)字符向右对齐,用str补齐长度 rjust(len,str)字符中间对齐,用s...

python3使用urllib示例取googletranslate(谷歌翻译)

复制代码 代码如下:#!/usr/bin/env python3# -*- coding: utf-8 -*-# File Name : gt1.py# Purpose :# Creat...