Django-allauth:无法反向匹配url

4
我已经安装了Django-allauth并仔细按照每个步骤操作: Settings.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"

url.py

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

然而,当我运行它时,在http://localhost:8000/accounts/处出现了404错误。
我尝试手动进行反向匹配:
./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.

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

谢谢。你说得对。然而,我仍然在http://localhost:8000/accounts/处收到404错误。你有什么想法是出了什么问题吗? - Houman
2个回答

3
当您使用reverse时,应传递视图名称而不是视图url。在Google的情况下,应该是reverse("google_login")。这解释了您的NoReverseMatch错误。
然而,我仍然在http://localhost:8000/accounts/处收到404错误,请问可能出了什么问题?
/accounts/只是无效的URL,所以404是正确的。请使用/accounts/login/。

1
我认为这应该在文档中提到,因为它让我感到困惑,以为django-allauth的某部分出了问题。 - KingFu

1

Allauth 的 URL 名称通常是这样的:account_login、account_logout、account_change_password 等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接