Search code examples
pythondjangofacebook-logindjango-allauth

Django Allauth Error during template rendering: NoReverseMatch at /accounts/login/


I`m stuck with this annoying problem and will be grateful if somebody could suggest something that actually helps. I have searched StackOverflow for similar questions and realized that none of the proposed solutions work for me. I started getting this error after Python and Django update (before the update all worked just fine). Now when trying to open the login page I see this exception:

NoReverseMatch at /accounts/login/
Reverse for 'facebook_login_by_token' with arguments '()' and keyword     arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://localhost:8000/accounts/login/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'facebook_login_by_token' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: D:\Python_3_4_3\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable:  D:\Python_3_4_3\python.exe
Python Version: 3.4.3
Python Path:    
['D:\\Django\\dentstudio',
'C:\\Windows\\system32\\python34.zip',
'D:\\Python_3_4_3\\DLLs',
'D:\\Python_3_4_3\\lib',
'D:\\Python_3_4_3',
'D:\\Python_3_4_3\\lib\\site-packages']
Server time:    Чт, 21 Май 2015 18:23:57 +0200

These are my projects settings:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'appointment',
'staff',
'job',
'news',
'articles',)


MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',)

TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

TEMPLATE_DIRS = ( 
TEMPLATE_PATH,)

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.contrib.messages.context_processors.messages",
"django.core.context_processors.request",    
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",)

AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend")

SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = {
'facebook': {
    'SCOPE': ['email', 'publish_stream'],
    'METHOD': 'js_sdk'  }}

SITE_ID=7

This is the template that raises the exception:

In template D:\Django\dentstudio\templates\socialaccount\snippets\login_extra.html, error at line 3

1   {% load socialaccount %}
2   
3   {% providers_media_js %}

My urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from home import views
from django.conf import settings

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^home/', include('home.urls')),
url(r'^appointment/', include('appointment.urls')),
url(r'^people/', include('staff.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^inside/', views.inside, name='inside'),
url(r'^chat/', views.chat, name='chat'),
url(r'^accounts/', include('allauth.urls')),
url(r'^job/', include('job.urls')),
url(r'^news/', include('news.urls')),
url(r'^prices/', views.prices, name='prices'),
url(r'^services/', views.services, name='services'),
url(r'^contacts/', views.contacts, name='contacts'),
url(r'^articles/', include('articles.urls')),

)

if settings.DEBUG:
  urlpatterns += patterns(
    'django.views.static',
    (r'^media/(?P<path>.*)',
    'serve',
    {'document_root': settings.MEDIA_ROOT}), )

My allauth urls.py:

from django.conf.urls import url, patterns, include
from home import views

try:
import importlib
except ImportError:
from django.utils import importlib

from allauth.socialaccount import providers

from . import app_settings

urlpatterns = patterns('', url('^', include('allauth.account.urls')),     url(r'^profile/', views.profile, name='profile'),)

if app_settings.SOCIALACCOUNT_ENABLED:
urlpatterns += patterns('', url('^social/',
                                include('allauth.socialaccount.urls')))

for provider in providers.registry.get_list():
try:
    prov_mod = importlib.import_module(provider.package + '.urls')
except ImportError:
    continue
prov_urlpatterns = getattr(prov_mod, 'urlpatterns', None)
if prov_urlpatterns:
    urlpatterns += prov_urlpatterns

I am already thinking of installing another app for facebook authorization, because just can`t handle this infuriating exception. Please help, thank you!


Solution

  • I had the same problem as you. After some intense debugging it seems that on my productions environment I did not have installed the requests. This package is used in every allauth.socialaccount.providers.* when the urls.py is imported(it is imported in the views.py).

    I have to mention that when I made a GET and /accounts/ my facebook urls were not present.

    So above you have and explanation for this.

    https://docs.djangoproject.com/en/dev/ref/urlresolvers/#django.core.urlresolvers.reverse

    "As part of working out which URL names map to which patterns, the reverse() function has to import all of your URLconf files and examine the name of each view. This involves importing each view function. If there are any errors whilst importing any of your view functions, it will cause reverse() to raise an error, even if that view function is not the one you are trying to reverse.

    Make sure that any views you reference in your URLconf files exist and can be imported correctly. Do not include lines that reference views you haven’t written yet, because those views will not be importable."

    The solution is to install the requests package: pip install requests

    The main idea is in the above documentation explanation. If you get that error it is likely that when you access /accounts/(in debug mode) you will not see your faulty links. So try to debug the urls.py for your app.

    Using django shell is pretty efficient.