Django中提示消息messages的设置
1. 引入messages模块
1 from django.contrib import messages
2. 把messages写入view中
1 @csrf_exempt
2 def search(request):
3 if request.method == 'GET':
4 bookname = request.GET.get('bookname')
5 bookinfo = Book.objects.filter(bookname=bookname)
6 is_staff = request.user.is_staff
7 username = request.user.username
8 gonggao = Gonggao.objects.all().order_by("-c_time")
9 liuyanban_all = liuyan.objects.all().order_by("-c_time")
10 c2 = JieInfo.objects.filter(u_name=username, book_name=bookname)
11 c3 = JieInfo.objects.filter(u_name=username).count()
12 if username == '':
13 messages.error(request, '亲,请您先登录,才能使用该功能。')
14 return redirect('/')
15 else:
16 if bookname == '':
17 messages.error(request, '亲,搜索内容不能为空哦。')
18 return redirect('/')
19 elif c2.exists():
20 messages.error(request, '对不起,您不能再次借阅该图书了。')
21 return redirect('/')
22 elif c3 == 5:
23 messages.error(request, '对不起,您将超出图书馆借阅图书数量限制。')
24 return redirect('/')
25 else:
26 if bookinfo.exists():
27 return render(request, "systeam/searched.html",
28 {'bookinfo': bookinfo, 'username': username, 'is_staff': is_staff})
29 else:
30 messages.error(request, '亲,没有这本书呢。')
31 return redirect('/')
32 else:
33 return render(request, "systeam/searched.html", {'username': username})
3. 把messages渲染到页面中
 1 {% if messages %}
 2
 3     <ul class="messages">
 4
 5     {% for message in messages %}
 6
 7
 8         <li{%if message.tags %} class="{{ message.tags }}"{% endif %}>
 9             <div class="m_title">
10                 {{ message.tags }}
11                 <a href="" id="a_tuichu">
12                     <i class="iconfont icon-fork"></i>
13                 </a>
14             </div>
15             <div id="m_box">
16                 {{ message }}
17             </div>
18             <a href="" id="a_sure">
19                 <button id="button_sure">
20                 确认
21                 </button>
22             </a>
23         </li>
24
25     {% endfor %}
26
27     </ul>
28
29     {% endif %}
Django中提示消息messages的设置的更多相关文章
- 设置EditText控件中提示消息hint的字体颜色和大小
		
设置EditText控件中提示消息hint的字体颜色和大小 1.设置字体大小 代码例: public void init(){ hint= (EditText) findViewById(R.id.i ...
 - Django中提示TemplateDoesNotExist?
		
用的是1.9版本.需要在settings.py文件中设置TEMPLATES下的DIRS如下: TEMPLATES = [ { 'BACKEND': 'django.template.backends. ...
 - Django 中的static文件的设置
		
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ('article',os.path.jo ...
 - Excel导出中HttpServletResponse消息头参数设置
		
response.setCharacterEncoding("UTF-8"); //编码格式为UTF-8 response.setContentType("applica ...
 - Django中的session和cookie及分页设置
		
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
 - django中向用户发送邮件信息
		
发送邮件来让用户激活,因此,邮件中需要包含用户信息.但用户信息需要加密才可以.因此加密采用的是itsdangerous中的TimedJSONWebSignatureSerializer. 参考链接:h ...
 - django中操作cookie与session
		
cookie 什么是Cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息. Cookie的 ...
 - Django中的分页,cookies与session
		
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
 - Django中的cookie和session
		
前言 HTTP协议 是短连接.且状态的,所以在客户端向服务端发起请求后,服务端在响应头 加入cokie响应给浏览器,以此记录客户端状态: cook是来自服务端,保存在浏览器的键值对,主要应用于用户登录 ...
 
随机推荐
- Go语言核心36讲(Go语言实战与应用十一)--学习笔记
			
33 | 临时对象池sync.Pool 到目前为止,我们已经一起学习了 Go 语言标准库中最重要的那几个同步工具,这包括非常经典的互斥锁.读写锁.条件变量和原子操作,以及 Go 语言特有的几个同步工具 ...
 - hutool的时间工具类
			
hutool的时间工具类 糊涂的时间工具类有很多使用方法,用到了这几个 日期向后偏移 String now = DateUtil.now(); Date date4= DateU ...
 - vcstool是什么?
			
为什么会去了解vcstool,在想要手动编译并且获取ROS源码的时候,有一个Get ROS 2 code的章节中使用到了这个工具. mkdir -p ~/ros2_foxy/src cd ~/ros2 ...
 - Codeforces 590E - Birthday(AC 自动机+Dilworth 定理+二分图匹配)
			
题面传送门 AC 自动机有时只是辅助建图的工具,真的 首先看到多串问题,果断建出 AC 自动机.设 \(m=\sum|s_i|\). 不难发现子串的包含关系构成了一个偏序集,于是我们考虑转化为图论,若 ...
 - Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)
			
题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...
 - Codeforces 1500E - Subset Trick(线段树)
			
Codeforces 题目传送门 & 洛谷题目传送门 一道线段树的套路题(似乎 ycx 会做这道题?orzorz!!11) 首先考虑什么样的 \(x\) 是"不合适"的,我 ...
 - char *p、char p[]、字符串的几个题目
			
总结一下遇到的关于char *p.char p[]和字符串的题目: 例一:(指针的指针) 1 void getmemory(char **p) 2 { 3 p = (char *)malloc(100 ...
 - Java设计模式之(十四)——策略模式
			
1.什么是策略模式? Define a family of algorithms, encapsulate each one, and make them interchangeable. Strat ...
 - 制作nc文件(Matlab)
			
首先看一个nc文件中包含哪些部分,例如一个标准的 FVCOM 输入文件 wind.nc: netcdf wind { dimensions: nele = 36858 ; node = 18718 ; ...
 - 【GS文献】植物育种中基因组选择的方法、模型及展望
			
目录 1. GS/GP在植物育种中的角色 2. GP模型应用 3. GP模型的准确性 4. 植物育种的GS展望 5. 小结 Genomic SelectioninPlant Breeding: Met ...