我正在尝试使用 django-social-auth 来实现 google openid 登录到我的应用程序中,我的问题是我收到此错误:
TemplateSyntaxError at /login
Caught ImportError while rendering: cannot import name get_backend
Request Method: GET
Request URL: #############################
Django Version: 1.3.1
Exception Type: TemplateSyntaxError
Exception Value:
Caught ImportError while rendering: cannot import name get_backend
Exception Location: /home/group018/web/WSProject/social_auth/views.py in <module>, line 19
Python Executable: /usr/bin/python2.7
Python Version:
在异常位置,您可以看到我的项目的层次结构。
view.py
文件如下:
from functools import wraps
from django.http import HttpResponseRedirect, HttpResponse,HttpResponseServerError
from django.core.urlresolvers import reverse
from django.contrib.auth import login, REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.utils.importlib import import_module
from django.views.decorators.csrf import csrf_exempt
from WSProject.social_auth.utils import sanitize_redirect, setting, log,backend_setting, clean_partial_pipeline
from WSProject.social_auth.backends import get_backend
DEFAULT_REDIRECT = setting('SOCIAL_AUTH_LOGIN_REDIRECT_URL') or setting('LOGIN_REDIRECT_URL')
LOGIN_ERROR_URL = setting('LOGIN_ERROR_URL', setting('LOGIN_URL'))
RAISE_EXCEPTIONS = setting('SOCIAL_AUTH_RAISE_EXCEPTIONS', setting('DEBUG'))
PROCESS_EXCEPTIONS = setting('SOCIAL_AUTH_PROCESS_EXCEPTIONS','social_auth.utils.log_exceptions_to_messages')
def dsa_view(redirect_name=None):
.
.
.
函数 get_backend 在 WSProject/social_auth/backends/__init__.py
中定义,我尝试将其导入为:
from WSProject.social_auth.backends.__init__ import get_backend
但它不起作用......有什么想法吗?
请注意,下面对我所说的导入正在运行
已解决:终于我做到了,问题是库,它们没有像 jpic 所说的那样正确安装。
最佳答案
你尝试过这个吗?
from social_auth.backends import get_backend
您不应在代码中对项目名称进行硬编码。这使得代码的可移植性较差。
如果这不起作用,那么你还没有正确安装 django-social-auth。修复您的设置:
创建一个 virtualenv,在项目的父目录中似乎很公平:
virtualenv/path/to/venv
激活 virtualenv:
source/path/to/venv/bin/activate
安装应用程序即。来自 git:
pip install -e git+git://github.com/omab/django-social-auth.git#egg=social_auth
我还在 django, virtualenv and pip 上写了一篇更详细的文章这应该是您感兴趣的。
关于python - 使用 django-social-auth 出现导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564685/