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变量问题的解决方法的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did81765