3

我已经安装了 Django-allauth 并仔细执行了每一步:

设置.py

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    'django.core.context_processors.request',
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",    
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

AUTHENTICATION_BACKENDS =  (
    'django.contrib.auth.backends.ModelBackend',    
    "allauth.account.auth_backends.AuthenticationBackend",
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',    
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    ...
)

ACCOUNT_AUTHENTICATION_METHOD="username_email"

网址.py

(r'^accounts/', include('allauth.urls')),

但是,当运行它时,我得到一个 404http://localhost:8000/accounts/

我试图手动反向匹配它:

./manage.py shell
from allauth.socialaccount.providers.google.urls import *

工作正常。

./manage.py shell
from django.core.urlresolvers import reverse
reverse('/accounts/google/login/')

然而这个失败了:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/kave/vc/cb-env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 476, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/home/kave/vc/cb-env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in _reverse_with_prefix
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for '/accounts/google/login/' with arguments '()' and keyword arguments '{}' not found.

我已经在虚拟环境中正确安装了它。可能出了什么问题?或者这是一个错误?

4

2 回答 2

3

当您使用 reverse 时,您应该传递视图名称,而不是视图 url。在谷歌的情况下,这将是反向(“google_login”)。这就解释了你的 NoReverseMatch

但是我仍然在http://localhost:8000/accounts/得到 404知道什么是错的吗?

/accounts/根本不是有效的 URL,因此 404 是正确的。采用/accounts/login/

于 2013-02-12T23:43:04.607 回答
0

Allauth url 名称如下:account_login、account_logout、account_change_password、..

于 2021-05-06T15:34:41.233 回答