[英]Django Context processors not working?
I have made one Django context processor which is not working...and it is also showing warning messages.First will show you the warning message:- 我制作了一个无法运行的Django上下文处理器...它还显示警告消息。首先将向您显示警告消息:-
WARNINGS: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. 警告:?:(1_8.W001)在Django 1.8中不赞成使用独立的TEMPLATE_ *设置,并且TEMPLATES字典优先。 You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_CONTEXT_PROCESSORS. 您必须将以下设置的值放入默认的TEMPLATES字典中:TEMPLATE_CONTEXT_PROCESSORS。
Now,i have created mym custum context processor this way 现在,我以这种方式创建了mym custum上下文处理器
in settings.py:- 在settings.py中:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"cms.utils.context_processors.permission_based_hidding_of_sidebar"
)
and created my fuction in utills,the custum context processors:- 并在实用程序,custum上下文处理器中创建了我的功能:-
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from cms.models.cmsUser import CmsUser
from cms.models.masterUsersPermissionTabMappings import MasterUsersPermissionTabMappings
@login_required
@csrf_exempt
def permission_based_hidding_of_sidebar(request):
cms_user = CmsUser.objects.get(userId=request.user.id)
print cms_user.id
universityPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=1)
cmsUserPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=2)
promotedPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=3)
appUserPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=4)
newsPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=5)
emailPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=6)
pushPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=7)
chatPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=8)
frontendPermission = MasterUsersPermissionTabMappings.objects.filter(userId=cms_user).get(permissionTypeId=9)
print universityPermission
a='hello'
return render(request,'templates/admin_user_management/admin_user_add.html',{
'universityPermission':universityPermission,'cmsUserPermission':cmsUserPermission,
'promotedPermission':promotedPermission,'appUserPermission':appUserPermission,
'newsPermission':newsPermission,'emailPermission':emailPermission,'pushPermission':pushPermission,
'chatPermission':chatPermission,'frontendPermission':frontendPermission,'sayHello':a
})
and i am trying to view it using this on my views:- 我试图在我的观点上使用它来查看它:
return render(request,template-name,{},context_instance=RequestContext(request))
it is showing me the error context_instance is not define in views.Is there is any way i can use it in views. 它向我显示错误context_instance未在views中定义。是否有任何方法可以在view中使用它。
Your permission_based_hidding_of_sidebar
method looks like a view, not a context processor. 您的permission_based_hidding_of_sidebar
方法看起来像一个视图,而不是上下文处理器。 A template context processor should return a dictionary, not an HTTP response from render()
It doesn't make sense for it to use login_required
or csrf_exempt
decorators. 模板上下文处理器应该返回字典,而不是来自render()
的HTTP响应。使用login_required
或csrf_exempt
装饰器对它没有意义。
In your view, you do not need context_instance=RequestContext(request)
, the render
shortcut will automatically use a request to render the template. 在您看来,您不需要context_instance=RequestContext(request)
, render
快捷方式将自动使用请求来渲染模板。
Finally, you can fix the 1_8.W001
warning by updating your settings to use TEMPLATES
. 最后,您可以通过更新设置以使用TEMPLATES
来修复1_8.W001
警告。 You should then update the context_processors
in OPTIONS
, instead of setting TEMPLATE_CONTEXT_PROCESSORS
. 然后,您应该在OPTIONS
更新context_processors
,而不是设置TEMPLATE_CONTEXT_PROCESSORS
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.