解决Django模板无法使用perms变量问题的方法

yipeiwu_com5年前Python基础

前言

本文主要给大家介绍了关于Django模板无法使用perms变量的解决方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

解决方法:

首先,在使用Django内置权限管理系统时,settings.py文件要添加

INSTALLED_APPS添加:
'django.contrib.auth',

 
MIDDLEWARE添加:
'django.contrib.auth.middleware.AuthenticationMiddleware',

'django.contrib.auth.context_processors.auth',
TEMPLATES = [
 {
  'BACKEND': 'django.template.backends.django.DjangoTemplates',
  'DIRS': [os.path.join(BASE_DIR, 'templates')],
  'APP_DIRS': True,
  'OPTIONS': {
   'context_processors': [
    'django.template.context_processors.debug',
    'django.template.context_processors.i18n',
    'django.template.context_processors.media',
    'django.template.context_processors.static',
    'django.template.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.template.context_processors.request',
    'django.contrib.auth.context_processors.auth',
   ],
  },
 },
]

如何在模板进行权限检查呢?

根据官网说明 https://docs.djangoproject.com/en/1.11/topics/auth/default/ ,已登录用户权限保存在模板{{ perms }}变量中,是权限模板代理django.contrib.auth.context_processors.PermWrapper的一个实例,具体可以查看django/contrib/auth/context_processors.py源码

测试用例:

 

测试过程中,发现{{ perms }}变量压根不存在,没有任何输出;好吧,只能取Debug Django的源码了

def auth(request):
 """
 Returns context variables required by apps that use Django's authentication
 system.

 If there is no 'user' attribute in the request, uses AnonymousUser (from
 django.contrib.auth).
 """
 if hasattr(request, 'user'):
  user = request.user
 else:
  from django.contrib.auth.models import AnonymousUser
  user = AnonymousUser()
 print(user, PermWrapper(user), '-----------------------')
 return {
  'user': user,
  'perms': PermWrapper(user),
 }

测试访问接口,发现有的接口有打印权限信息,有的没有,似乎恍然醒悟

可以打印权限信息的接口返回:

 return render(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error})

不能打印权限新的接口返回:

 return render_to_response( 'fms/fms.html', data)

render和render_to_response区别

render是比render_to_reponse更便捷渲染模板的方法,会自动使用RequestContext,而后者需要手动添加:

return render_to_response(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error},context_instance=RequestContext(request))

其中RequestContext是django.template.Context的子类.接受requestcontext_processors ,从而将上下文填充渲染到模板问题已经很明确,由于使用了render_to_response方法,没有手动添加context_instance=RequestContext(request)导致模板不能使用{{ perms }}变量

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python正则表达式使用范例分享

作为一个概念而言,正则表达式对于Python来说并不是独有的。但是,Python中的正则表达式在实际使用过程中还是有一些细小的差别。 本文是一系列关于Python正则表达式文章的其中一部...

Flask使用Pyecharts在单个页面展示多个图表的方法

Flask使用Pyecharts在单个页面展示多个图表的方法

在Flask页面展示echarts,主要有两种方法: 方法1、原生echarts方法 自己在前端引入echarts.js文件、自己创建div、自己初始化echarts对象、自己从官网复制...

python实现简易数码时钟

python实现简易数码时钟

最近迷上了Python,要说为什么呢?Python语法简单,功能强大,有广泛的第三方库能快速编程实现自己的想法(无需重复去造轮子)。就像某位前辈说的:“人生苦短,学会偷懒…”,配置好su...

python数据结构树和二叉树简介

一、树的定义 树形结构是一类重要的非线性结构。树形结构是结点之间有分支,并具有层次关系的结构。它非常类似于自然界中的树。树的递归定义:树(Tree)是n(n≥0)个结点的有限集T,T为空...

python实现名片管理系统

python实现名片管理系统

本文实例为大家分享了python实现名片管理系统的具体代码,供大家参考,具体内容如下 系统需求 程序启动,显示名片管理系统欢迎界面,并显示功能菜单 ******************...