Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named django.contrib.auth when using things that redirect

I get the ImportError "No module named django.contrib.auth" both when I try to use the django.shortcuts redirect function and when I try to use:

(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),

I figure it can't be a coincidence that the only place I'm hitting this error is in places where the page is redirected, but maybe it is. I know that the user isn't actually getting logged out, so the error happens before you even get to any redirect code.

Below is my urls.py file.

import django.contrib.auth.views
from django.conf.urls.defaults import *
import django.contrib.auth

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('karma.views',
(r'^$', 'homepage'),
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
(r"^opportunities/nearby$", 'draw_map'),


(r'^admin/', include(admin.site.urls)),
url(r'', include('social_auth.urls')),
(r'^profile/', include('karmup.profile.urls')),
)
like image 620
Jason Avatar asked Jun 06 '12 07:06

Jason


1 Answers

You are mixing up URL prefixes in your urlpatterns.

urlpatterns = patterns('karma.views',
  (r'^$', 'homepage'),
  (r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
  (r"^opportunities/nearby$", 'draw_map'),
)

Django tries to find views relative to the given URL prefix, in your case 'karma.views'. Inside this module, there is no 'django.contrib.auth.views.logout', hence you get the ImportError.

Move the logout URL to a second block, e.g.:

urlpatterns += patterns('',
  (r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
)

That should resolve your issue.

like image 113
cfedermann Avatar answered Oct 12 '22 13:10

cfedermann