django-cms login_required不起作用

时间:2014-01-14 15:11:46

标签: django django-cms

Django == 1.5.4

Django-CMS == 2.4.2

页面标记“login_required”无效。它仅适用于主页面,但在所有内页面中它没有。 cms插件中没有任何代码修改。

“login_required”仅检入一个文件: /site-packages/cms/views.py 排在 136

>

# permission checks
if page.login_required and not request.user.is_authenticated():
    return redirect_to_login(urlquote(request.get_full_path()), settings.LOGIN_URL)

我设置

print "check"

在它之上,正如预期的那样 - 它只在主页面打印......

任何想法可能是什么问题?

1 个答案:

答案 0 :(得分:0)

问题的原因在相关评论中找到。我通过编写自定义装饰器来解决它,它检查了cms页面的“login required”标志:

views.py(app_hook)

from django.shortcuts import render
from project.decorators import check_login_required_flag

@check_login_required_flag
def index(request):
    """ private page index """    
    return render(request, 'private_room/index.html', {})

<强> decorators.py

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from cms.utils.page_resolver import get_page_from_request

def check_login_required_flag(view_func):
    """
    Decorator checks 'login required' flag of a cms-page and redirect
    unauthorized user to login page
    """
    def _wrapped_view_func(request, *args, **kwargs):
        page = get_page_from_request(request)
        if page and page.login_required:
            if not request.user.is_authenticated():
                return HttpResponseRedirect(reverse('login')+'?next='+request.path)
        return view_func(request, *args, **kwargs)
    return _wrapped_view_func