I followed http://django-rest-auth.readthedocs.io/en/latest/installation.html#registration-optional
As django-rest-auth installation doc said, i installed django-allauth first, but url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
NameError: name 'FacebookLogin' is not defined
comes, and i don't know how to solve this.
blog/views.py url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'), NameError: name 'FacebookLogin' is not defined
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import filters
from rest_framework import generics
from django.contrib.auth.decorators import login_required
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class FacebookLogin(SocialLoginView):
adapter_class = FacebookOAuth2Adapter
blog/urls.py
from django.conf.urls import url, patterns
from blog import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blog/$', views.post_list, name='list'),
url(r'^blog/create/$', views.post_create, name='create'),
url(r'^blog/(?P<pk>[0-9]+)$', views.post_detail, name='detail'),
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'django.contrib.sites',
'blog',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
]
SITE_ID = 1
When i command python3 manage.py runserver,
it gives me
python3 manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f65f81601e0>
Traceback (most recent call last):
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/keepair/djangogirls/mysite/urls.py", line 21, in <module>
url(r'', include('blog.urls')),
File "/home/keepair/djangogirls/myvenv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/keepair/djangogirls/blog/urls.py", line 14, in <module>
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
NameError: name 'FacebookLogin' is not defined
Why this error has been occurred?
What is my fault?
Thanks for reading.
Error is self explanatory that FacebookLogin
is not defined in urls.py
.
Fault - You are using FacebookLogin
but haven't imported it in the urls.py
.
Solution:
You have imported blog.views in the urls.py, so replace FacebookLogin.as_view()
with views.FacebookLogin.as_view()
from django.conf.urls import url, patterns
from blog import views
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = [
url(r'^blog/$', views.post_list, name='list'),
url(r'^blog/create/$', views.post_create, name='create'),
url(r'^blog/(?P<pk>[0-9]+)$', views.post_detail, name='detail'),
url(r'^rest-auth/facebook/$', views.FacebookLogin.as_view(), name='fb_login'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
OR
import the class itself from blog.views import FacebookLogin
.