使用Google身份验证时出现Python Social Auth-导入错误

时间:2014-10-27 17:53:49

标签: python django python-social-auth

我在一个多月前成功跟进了这篇博文:http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/

当我创建新环境并重新创建步骤时,我现在在模板渲染过程中遇到导入错误no module named google_auth

它追溯到这一行:

<a href="{% url 'social:begin' 'google-oauth2' %}?next={{ request.path }}">Login</a>

看起来django没有在社交应用中查找正确的模板处理?关于为什么不再有效的任何想法?

#settings.py
INSTALLED_APPS = (... , 'social.apps.django_app.default', ...)

AUTHENTICATION_BACKENDS = ('social.backends.google.GoogleOAuth2', 'django.contrib.auth.backends.ModelBackend')

TEMPLATE_CONTEXT_PROCESSORS = ('django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect')

#urls.py
urlpatterns = patterns('', 
url('', include('social.apps.django_app.urls', namespace='social')),
url('', include('django.contrib.auth.urls', namespace='auth')),
url(r'^$', 'app.views.home', name='home'),
)

编辑:

下面的完整追溯
Environment:
Request Method: GET

Django Version: 1.7
Python Version: 2.7.8
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'charts',
 'social.apps.django_app.default',
 'djcelery',
 'kombu.transport.django')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
 'charts.middleware.SocialAuthExceptionMiddleware')


Template error:
In template charts/templates/base.html, error at line 69
   No module named google_auth
   59 :           </ul>


   60 :         </li>


   61 :         <li>


   62 :           Hello, <strong>{{ user.get_full_name|default:user.username }}</strong>!


   63 :         </li>


   64 :         <li>


   65 :           <a href="{% url 'auth:logout' %}?next={{ request.path }}">Logout</a>


   66 :         </li>


   67 :         {% else %}


   68 :         <li>


   69 :           <a href=" {% url 'social:begin' 'google-oauth2' %} ?next={{ request.path }}">Login</a>


   70 :         </li>


   71 :         {% endif %}


   72 :       </ul>


   73 :     </nav>


   74 : </head>


   75 : <body>


   76 : {% block content %}


   77 : {% endblock %}


   78 : <div id="footer">


   79 :     <center>


Traceback:
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/charts/views.py" in home
  743.                              context_instance=context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
  23.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  174.         return t.render(context_instance)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  312.                 return nodelist.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  444.             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  517.                 app_list = resolver.app_dict[ns]
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/urlresolvers.py" in app_dict
  329.             self._populate()
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
  303.                 lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/urlresolvers.py" in callback
  231.         self._callback = get_callable(self._callback_str)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/utils/lru_cache.py" in wrapper
  101.                     result = user_function(*args, **kwds)
File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/urlresolvers.py" in get_callable
  101.                     not module_has_submodule(import_module(parentmod), submod)):
File "//anaconda/envs/analytics/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)

Exception Type: ImportError at /
Exception Value: No module named google_auth

2 个答案:

答案 0 :(得分:4)

如果它是你的选择,我建议改用django-allauth。我使用了django-allauth和Python Social Auth,并亲自发现使用django-allauth更容易。所以,如果你有机会,试一试

答案 1 :(得分:2)

这是一个难题。

错误,堆栈跟踪的第一行给出了一个线索:&#34; Traceback:.... wrapped_callback ....&#34; social.apps.django_app.views.auth视图被调用并包含在装饰器psa中。这最终会产生错误,但我认为django.core.handlers.base.get_response隐藏了实际的异常源。我认为重新加注会使堆栈跟踪不完整。

异常可能发生在from social.backends.utils.get_backend中。但是从远处调试很难。

您可以在文件response = wrapped_callback(request, *callback_args, **callback_kwargs)中的django.core.handlers.base行周围注释掉try-catch。加载页面时获得的新堆栈跟踪是什么?