RealmTeam / django-rest-framework-social-oauth2

python-social-auth and oauth2 support for django-rest-framework
MIT License
1.06k stars 191 forks source link

NoReverseMatch error when loging into admin page. #16

Closed dsrizvi closed 9 years ago

dsrizvi commented 9 years ago

Have a legitimate issue this time. When I go to the django admin site, I get this error

`Environment:

Request Method: POST Request URL: http://localhost:8000/admin/login/?next=/admin/

Django Version: 1.8.4 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', 'rest_framework', 'oauth2_provider', 'users', 'social.apps.django_app.default', 'rest_framework_social_oauth2') Installed Middleware: ('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', 'django.middleware.security.SecurityMiddleware')

Traceback: File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response

  1. response = wrapped_callback(request, _callback_args, *_callback_kwargs) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  2. response = view_func(request, _args, *_kwargs) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/admin/sites.py" in login
  3. return login(request, **defaults) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  4. return view(request, _args, *_kwargs) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  5. response = view_func(request, _args, *_kwargs) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  6. response = view_func(request, _args, *_kwargs) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/auth/views.py" in login
  7. if form.is_valid(): File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
  8. return self.is_bound and not self.errors File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in errors
  9. self.full_clean() File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  10. self._clean_form() File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/forms/forms.py" in _clean_form
  11. cleaned_data = self.clean() File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/auth/forms.py" in clean
  12. password=password) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/auth/init.py" in authenticate
  13. for backend, backend_path in _get_backends(return_tuples=True): File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/auth/init.py" in _get_backends
  14. backend = load_backend(backend_path) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/contrib/auth/init.py" in load_backend
  15. return import_string(path)() File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/utils/module_loading.py" in import_string
  16. module = import_module(module_path) File "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py" in import_module
  17. import(name) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/rest_framework_social_oauth2/backends.py" in
  18. class DjangoOAuth2(BaseOAuth2): File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/rest_framework_social_oauth2/backends.py" in DjangoOAuth2
  19. AUTHORIZATION_URL = reverse('authorize') File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  20. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, _args, *_kwargs))) File "/Users/danyalrizvi/Google_Drive/Work/gigster/blackbox/blackbox-virtualenv/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  21. (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /admin/login/ Exception Value: Reverse for 'authorize' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []'

From my understanding the offending line is AUTHORIZATION_URL = reverse('authorize') called by class DjangoOAuth2(BaseOAuth2):. I don't understand why the rest_framework_social_oauth2 is involved at all with the default django admin.

dsrizvi commented 9 years ago

Here are my settings:

""" Django settings for beerstash project.

Generated by 'django-admin startproject' using Django 1.8.4.

For more information on this file, see https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """

Build paths inside the project like this: os.path.join(BASE_DIR, ...)

import os from config import *

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

Quick-start development settings - unsuitable for production

See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY =***

SECURITY WARNING: don't run with debug turned on in production!

DEBUG = True

ALLOWED_HOSTS = []

Application definition

INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'oauth2_provider', 'users', 'social.apps.django_app.default', 'rest_framework_social_oauth2',

)

AUTHENTICATION_BACKENDS = (

# Facebook OAuth2
'social.backends.facebook.FacebookAppOAuth2',
'social.backends.facebook.FacebookOAuth2',

# django-rest-framework-social-oauth2
'rest_framework_social_oauth2.backends.DjangoOAuth2',

# Django
'django.contrib.auth.backends.ModelBackend',

)

SOCIAL_AUTH_PIPELINE = ( 'social.pipeline.social_auth.social_details', 'social.pipeline.social_auth.social_uid', 'social.pipeline.social_auth.auth_allowed', 'social.pipeline.social_auth.social_user', 'social.pipeline.user.get_username', 'social.pipeline.user.create_user', 'social.pipeline.social_auth.associate_user', 'social.pipeline.social_auth.load_extra_data', 'social.pipeline.user.user_details'

'users.pipeline.user_details'

)

REST_FRAMEWORK = {

'DEFAULT_AUTHENTICATION_CLASSES': (
    # OAuth
    'oauth2_provider.ext.rest_framework.OAuth2Authentication',
    'rest_framework_social_oauth2.authentication.SocialAuthentication',
)

}

OAUTH2_PROVIDER = {

this is the list of available scopes

'SCOPES': {'read': 'Read scope', 'write': 'Write scope'}

}

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', 'django.middleware.security.SecurityMiddleware', )

ROOT_URLCONF = 'blackbox_server.urls'

TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'social.apps.django_app.context_processors.backends', 'social.apps.django_app.context_processors.login_redirect',

        ],
    },
},

]

WSGI_APPLICATION = 'blackbox_server.wsgi.application'

Database

https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }

Social

SOCIAL_AUTH_FACEBOOK_KEY = *** SOCIAL_AUTH_FACEBOOK_SECRET = ** SOCIAL_AUTH_FACEBOOK_SCOPE = [ 'email', 'user_friends', 'friends_location', ] SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy' SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/' SOCIAL_AUTH_LOGIN_ERROR_URL = '/'

LOGIN_ERROR_URL = '/'

AUTH_PROFILE_MODULE = "users.UserProfile"

TEMPLATE_DIRS = (

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

)

Internationalization

https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Static files (CSS, JavaScript, Images)

https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

Here is my urls.py:

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

urlpatterns = patterns( '',

dashboard pages

url(r'^$', 'dashboard.views.login'),
# # url(r'^home/$', 'dashboard.views.home'),
# # url(r'^logout/$', 'dashboard.views.logout'),
# # authentication
url('', include('social.apps.django_app.urls', namespace='social')),
url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),

# my apps
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls')),

)

dsrizvi commented 9 years ago

Nevermind, figured it out.

RusEu commented 9 years ago

what happend ? same problem here..

m-misseri commented 9 years ago

same here.

RusEu commented 9 years ago

bendspax : add this lines to your url.py file:

url(r'^social/', include('social.apps.django_app.urls', namespace='social')),
url(r'^auth/', include('rest_framework_social_oauth2.urls')),

This should solve the problem :)

haseebehsan commented 4 years ago

bendspax : add this lines to your url.py file:

url(r'^social/', include('social.apps.django_app.urls', namespace='social')),
url(r'^auth/', include('rest_framework_social_oauth2.urls')),

This should solve the problem :)

Also, just adding on to this, make sure you have added this in settings.py file

AUTHENTICATION_BACKENDS = (
    # Others auth providers (e.g. Google, OpenId, etc)

    # Facebook OAuth2
    'social_core.backends.facebook.FacebookAppOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',

    # django-rest-framework-social-oauth2
    'rest_framework_social_oauth2.backends.DjangoOAuth2',

    # Django
    'django.contrib.auth.backends.ModelBackend',
)
hounfodji commented 5 months ago

In the settings.py, add this line at the end:

DRFSO2_URL_NAMESPACE = "drfso2"