Search code examples
pythondjangodjango-context

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:-

WARNINGS: ?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_CONTEXT_PROCESSORS.

Now,i have created mym custum context processor this way

in 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:-

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.


Solution

  • Your permission_based_hidding_of_sidebar method looks like a view, not a context processor. 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.

    In your view, you do not need context_instance=RequestContext(request), the render shortcut will automatically use a request to render the template.

    Finally, you can fix the 1_8.W001 warning by updating your settings to use TEMPLATES. You should then update the context_processors in OPTIONS, instead of setting TEMPLATE_CONTEXT_PROCESSORS.