django request.user.is_authenticated总是如此?

zen*_*enk 10 python django

任何人都可以告诉我为什么在以下代码中我被重定向到yahoo.com而不是google.com?

网址

urlpatterns = patterns('', (r'^$', initialRequest,))
Run Code Online (Sandbox Code Playgroud)

视图

def initialRequest(request):

    if request.user.is_authenticated:
        return HttpResponseRedirect('http://yahoo.com')
    else:
        return HttpResponseRedirect('http://google.com')
Run Code Online (Sandbox Code Playgroud)

Ric*_*een 43

不应该是request.user.is_authenticated()括号,因为它是一个功能?

对于Django 1.10 +

is_authenticated 现在是一个属性(虽然它现在保持向后兼容).


dm0*_*514 10

正如Richard所提到的,is_authenticated是一个函数,所以在你的视图中它应该被称为:request.user.is_authenticated().

由于django模板语言可能存在混淆,因为在模板中调用它会使其显示为属性而不是方法.

{{ user.is_authenticated}} https://docs.djangoproject.com/en/dev/topics/auth/