繁体   English   中英

在 /login/authenticated 处获取 'AttributeError','User' object 没有属性 'backend'

[英]Getting a 'AttributeError at /login/authenticated', 'User' object has no attribute 'backend'

我正在使用此处找到的 Python-Oauth2 库 Django 1.3。 我最终将使用位于此处的 Twitter 库。

我已经搜索了所有我能找到的东西,但我不明白为什么我会收到这个错误。 当其他人问这个问题时,他们被告知这是因为他们没有调用身份验证(),但我在登录名上方有这个问题。

我已经设法弄清楚它失败的原因以某种方式被吸引到使用 Django 对 authenticate() 的调用中。 我通过注释掉登录来确认这一点 - 这会立即消除错误。 有关该身份验证的某些内容,无论是个人资料/用户信息还是该性质的某些内容,都失败了。

这是我的views.py中的代码:

def twitter_authenticated(request):
    # Step 1. Use the request token in the session to build a new client.
    token = oauth.Token(request.session['request_token']['oauth_token'],
                        request.session['request_token']['oauth_token_secret'])
    client = oauth.Client(consumer, token)

    # Step 2. Request the authorized access token from Twitter.
    resp, content = client.request(access_token_url, "GET")
    if resp['status'] != '200':
        print content
        raise Exception("Invalid response from Twitter.")

    """
    This is what you'll get back from Twitter. Note that it includes the
    user's user_id and screen_name.
    {
        'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M',
        'user_id': '120889797',
        'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD',
        'screen_name': 'heyismysiteup'
    }
    """

    access_token = dict(cgi.parse_qsl(content))

    # Step 3. Lookup the user or create them if they don't exist.
    try:
        user = User.objects.get(username=access_token['screen_name'])
    except User.DoesNotExist:
        # When creating the user I just use their screen_name@twitter.com
        # for their email and the oauth_token_secret for their password.
        # These two things will likely never be used. Alternatively, you
        # can prompt them for their email here. Either way, the password
        # should never be used.
        user = User.objects.create_user(access_token['screen_name'], '%s@twitter.com' % access_token['screen_name'], access_token['oauth_token_secret'])

        # Save our permanent token and secret for later.
        profile = Profile()
        profile.user = user
        profile.oauth_token = access_token['oauth_token']
        profile.oauth_secret = access_token['oauth_token_secret']
        profile.save()

    # Authenticate the user and log them in using Django's pre-built
    # functions for these things.

    user = authenticate(username=access_token['screen_name'], password=access_token['oauth_token_secret'])
    login(request, user)

    return HttpResponseRedirect('/')

实际上,问题是我的身份验证视图中的身份验证调用 function 由于密码(实际上是access_token['oauth_token_secret'] )而失败。

所以我们在同一个页面上,我说的是def twitter_authenticated(request): function,它包含在 python-oauth2 github 页面本身,在代码示例中,在视图代码部分中。

由于我还升级了其他所有内容以及 Django 1.3,因此我在auth_user表中输入了错误的密码。

总结一下:只需在加载页面时查看 Django 调试 output (我假设您的设置中有DEBUG = True ),然后将其缩小到确切的调用。 我很确定对你来说失败的 function 对我来说是失败的,不过,它是

user = authenticate(username='blahblah', password=access_token['oauth_token_secret'])

因此,请确保 auth_user 表/密码字段是您的access_token['oauth_token_secret']的正确 SHA1。

我弄错的原因...我有一台新计算机,所以我安装了所有最新的东西,但没有从旧计算机中继承数据库数据。

我有同样的问题。 我刚刚注意到 Python-Oauth2 的 github 页面有以下与views.py文件相关的消息:

“注意:以下代码是为 Python 2.4 编码的,因此如果您使用 Python 2.6+,可能需要更新此处的某些库和代码。”

我想我会尝试暂时降级到 2.5,以缩小它是 django 1.3 还是 python 的范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM