好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

这篇文章主要给大家介绍了关于解决Django模板无法使用perms变量问题的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

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',
   ],
  },
 },
] 
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_to_response(request, 'fms/fms_add.html', {'request': request, 'form': form, 'error': error},context_instance=RequestContext(request)) 

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

以上就是有关Django模板无法使用perms变量问题的解决方法的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于有关Django模板无法使用perms变量问题的解决方法的详细内容...

  阅读:71次