我正在使用django-allauth库将Facebook和Google登录集成到我的网站(在localhost上)。我一直无法实现Facebook登录功能(here is my unanswered question,所以我尝试对Google进行同样的操作。
有两个相互关联的问题:
1)我的注册页面从不加载,显示错误:
/ ResignMatch在/ signup /找不到与“ google_login”相反的内容。 'google_login'不是有效的视图函数或模式名称。
2)我的Facebook和Google按钮不可单击。他们什么都不做。
我有以下代码。
在settings.py:
中ALLOWED_HOSTS = [
'127.0.0.1',
'localhost',
]
INSTALLED_APPS = [
...
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
]
...
...
...
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"accounts.backends.EmailAuthenticationBackend",
"allauth.account.auth_backends.AuthenticationBackend",
]
SITE_ID=1
SOCIALACCOUNT_PROVIDERS = \
{'facebook':
{'METHOD': 'oauth2',
'SCOPE': ['email','public_profile', 'user_friends'],
'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
'FIELDS': [
'id',
'email',
'name',
'first_name',
'last_name',
'verified',
'locale',
'timezone',
'link',
'gender',
'updated_time'],
'EXCHANGE_TOKEN': True,
'LOCALE_FUNC': lambda request: 'kr_KR',
'VERIFIED_EMAIL': False,
'VERSION': 'v2.4'},
'google':
{'SCOPE': ['profile', 'email',],
'AUTH_PARAMS': {'access_type': 'online',}}}
在urls.py中(一般项目的url,但是我尝试分别为account.urls设置此设置,但也无济于事):
urlpatterns = [
...
path('', include('accounts.urls')),
path('accounts/', include('allauth.urls')),
path('admin/', admin.site.urls),
]
在signup.html:
中{% load socialaccount %}
{% providers_media_js %}
...
<a class="facebook-login" href="{% provider_login_url "facebook" method="js_sdk" %}">
<i class="ion-logo-facebook"></i>
<div>Sign up with Facebook</div>
...
<a class="google-login" href="{% provider_login_url "google" %}">
<i class="ion-logo-google"></i>
<div>Sign up with Google</div>
...
我已在Django管理员以及Google和Facebook开发人员中配置了所有内容。
如果将Facebook方法从js_sdk更改为oauth2,则会收到相同的NoReverseMatch错误。我曾尝试将名称空间添加到URL模式,但也无济于事。
有人知道吗?
UPD。我的整个urls.py文件如下:
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('movies.urls')),
path('', include('accounts.urls')),
path('accounts/', include('allauth.urls')),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
问题不在于代码。我的venv中没有安装requests。因此,它崩溃了一切。安装requests后,一切正常。