5

I successfully followed along this blog entry more than a month ago:http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/

When I created a new environment, and recreated the steps, I'm now getting an import error, no module named google_auth during template rendering.

It traces back to this line:

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

It looks like django is not looking in the social app for the correct template processing? Any ideas on why this is no longer working?

#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'),
)

EDIT: Full traceback below

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
DataSwede
  • 5,251
  • 10
  • 40
  • 66
  • Shouldn't it be `url 'social:begin'...` – aldo_vw Oct 27 '14 at 19:42
  • You are correct. it was in fact 'social:begin' in my actual html. Fixed question. – DataSwede Oct 27 '14 at 19:56
  • Import error can be raised due to import error in the recursive dependencies, which may not show on the django runserver log. Go into django shell and then try import the app . It will show you the correct error. – iamkhush Oct 27 '14 at 20:39
  • I am able to use manage.py shell and import social successfully. No errors are generated. – DataSwede Oct 28 '14 at 00:53
  • You have the full traceback for that import error? There's no ``google_auth`` import in ``python-social-auth`` – omab Oct 29 '14 at 02:23
  • @omab I added the full traceback above – DataSwede Oct 29 '14 at 04:02
  • What happens if you try ``{% url "social:begin" backend="google-oauth2" %}``? Do you have any reference to ``google_auth`` in your code? – omab Oct 30 '14 at 01:48
  • adding backend did not help. I have no references to google_auth in my code. The last line of the traceback is where the error seems to be coming from. Django is trying to import the module based on a import lib function they have – DataSwede Oct 30 '14 at 14:27
  • Do you have an app named ``google_auth``? What's the full value for ``INSTALLED_APPS``? – omab Nov 02 '14 at 01:39
  • There is no reference to `google_auth` within my django project. INSTALLED_APPS = ( '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', ) – DataSwede Nov 02 '14 at 18:47
  • If you're pretty confident you've followed the directions clearly in replicating the setup, my first two suspicions are: 1.) a dependency has updated since you installed it last, and you aren't pinned to a known-safe version (it may help to go hunt for what versions were current on pypi when you first followed the tutorial; it looks like at least django has released new versions in the past month), or 2.) You had to take some troubleshooting steps that you've forgotten about to get it working last time, but which aren't included in the article (possibly a path/pythonpath issue?). – abathur Nov 05 '14 at 01:59
  • It's almost completely a dependency issue. I just can't figure out which one. A lot of my libraries have been updated since then. Like > 30. Going through and trying to figure out the combination to make this work would probably take even longer – DataSwede Nov 06 '14 at 15:52

2 Answers2

4

If it is an option for you, I would propose to use django-allauth instead. I used both django-allauth and Python Social Auth and personally find it much easier to work with django-allauth. So if you have a chance, give it a try

Oleh Novikov
  • 558
  • 5
  • 17
2

This is one difficult problem.

The error, the first line of the stack trace gives a clue: "Traceback: .... wrapped_callback ...." The social.apps.django_app.views.auth view is called and is wrapped in decorator psa. This eventually produces the error but I think django.core.handlers.base.get_response is hiding the actual exception source. I think the reraise makes the stack trace incomplete at the moment.

The exception probably occurs in from social.backends.utils.get_backend. But it's hard to debug from a distance.

Can you comment out the try-catch around the line response = wrapped_callback(request, *callback_args, **callback_kwargs) in the file django.core.handlers.base. What is the new stack trace you get when loading the page?

rrmoelker
  • 152
  • 10
  • I threw a valueerror this time. Charts is the name of the app Traceback: File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 131. % (callback.__module__, view_name)) Exception Type: ValueError at / Exception Value: The view charts.views.home didn't return an HttpResponse object. It returned None instead. – DataSwede Nov 06 '14 at 16:00
  • Hmm, that is less usefull than expected. I did however notice that my main suspect, the `psa` wrapper, has "recently" had an update. That commit even reads "Refactor backend/strategy to avoid circular dependency". src: https://github.com/omab/python-social-auth/blob/master/social/backends/utils.py That commit might not have helped or even introduce a new circular dependency problem in your setup. Now it seems this commit is used from versions 0.2.0 and up. So let me suggest downgrading you package: `pip install "python-social-auth<0.2.0"`. BTW: what is your Django and python-social version? – rrmoelker Nov 06 '14 at 19:16
  • django is `1.7`. I downgraded to python social auth `0.1.26` at the start of this problem in order to see if there was a recent problem with that library, however have not had success. Perhaps `0.2.0` may be better. I'll try that – DataSwede Nov 06 '14 at 19:39
  • My suggestion is to try a version less than version `0.2.0`, otherwise it defeats the point. And if possible try Django 1.6. In my experience many packages lag behind Django changes (although I don't expect python-social-auth to be one of those). If this doesn't work, I'm out of ideas. – rrmoelker Nov 07 '14 at 08:02
  • 1
    I downgraded to `Django 1.6`, and am now getting a new error.. Still an import error, but it's different. 40. __import__(name) File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/contrib/contenttypes/admin.py" in 7. from django.contrib.contenttypes.fields import GenericForeignKey File "//anaconda/envs/analytics/lib/python2.7/site-packages/django/contrib/contenttypes/fields.py" in 13. from django.db.models.sql.datastructures import Col Exception Type: ImportError at / Exception Value: cannot import name Col – DataSwede Nov 07 '14 at 21:40