Django Context处理器无法正常工作?

时间:2017-05-24 09:00:36

标签: python django django-context

我制作了一个不能正常运行的Django上下文处理器......它还显示警告信息。首先会显示警告信息: -

  

警告:?:( 1_8.W001)独立的TEMPLATE_ *设置是   在Django 1.8和TEMPLATES字典中弃用   优先。您必须将以下设置的值放入   您的默认TEMPLATES dict:TEMPLATE_CONTEXT_PROCESSORS。

现在,我已经以这种方式创建了mym custum上传处理器

在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"
)

并在utills,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
    })

我试图在我的观点中使用它来查看它: -

return render(request,template-name,{},context_instance=RequestContext(request))

它向我显示错误context_instance未在views中定义。我有什么方法可以在视图中使用它。

1 个答案:

答案 0 :(得分:2)

您的permission_based_hidding_of_sidebar方法看起来像一个视图,而不是上下文处理器。模板上下文处理器应返回字典,而不是来自render()的HTTP响应。使用login_requiredcsrf_exempt装饰器对它没有意义。

在您看来,您不需要context_instance=RequestContext(request)render快捷方式会自动使用请求来呈现模板。

最后,您可以通过更新设置to use TEMPLATES来修正1_8.W001警告。然后,您应该更新context_processors中的OPTIONS,而不是设置TEMPLATE_CONTEXT_PROCESSORS