Django rest AllAuth:/auth/registration/ 没有名为 account.adapter 的模块导入错误

Django rest AllAuth: ImportError at /auth/registration/ No module named account.adapter

我正在尝试制作一个自定义适配器来编辑 allauth.account.adapter 中的 send_mail 函数(按照此说明 http://django-allauth.readthedocs.org/en/latest/advanced.html#custom-redirects),但是当我执行该函数时它不能找到帐户模块。这是回溯:

Environment:
Request Method: POST
Request URL: http://localhost:8000/auth/registration/

Django Version: 1.8.3
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'api',
 'rest_framework',
 'rest_framework_swagger',
 'rest_framework.authtoken',
 'rest_auth',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_auth.registration',
 'djrill')
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 "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  456.             response = self.handle_exception(exc)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  453.             response = handler(request, *args, **kwargs)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/rest_auth/registration/views.py" in post
  43.         if self.form.is_valid():
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
  184.         return self.is_bound and not self.errors
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/forms/forms.py" in errors
  176.             self.full_clean()
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  392.         self._clean_fields()
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
  410.                     value = getattr(self, 'clean_%s' % name)()
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/allauth/account/forms.py" in clean_username
  252.         value = get_adapter().clean_username(value)
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/allauth/account/adapter.py" in get_adapter
  359.     return import_attribute(app_settings.ADAPTER)()
File "/home/lccot/Documents/cursoslccbackend/env/local/lib/python2.7/site-packages/allauth/utils.py" in import_attribute
  119.     ret = getattr(importlib.import_module(pkg), attr)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/home/lccot/Documents/cursoslccbackend/cursoslccbackend/cursoslccbackend/adapters/allauth.py" in <module>
  2. from allauth.account.adapter import DefaultAccountAdapter

Exception Type: ImportError at /auth/registration/
Exception Value: No module named account.adapter

这是自定义适配器文件 (allauth.py):

from django.conf import settings
from allauth.account.adapter import DefaultAccountAdapter

class CustomAdapter(DefaultAccountAdapter):

    def send_mail(self, template_prefix, email, context):
        msg = self.render_mail(template_prefix, email, context)
        msg.send()

问题出在自定义适配器文件的名称中。它被命名为 "allauth.py"。只需将文件重命名为其他名称即可。

发生这种情况是因为 python 模块导入搜索按以下顺序工作:

  • 包含输入脚本的目录(或当前目录)。
  • PYTHONPATH(目录名列表,语法与
  • 相同
  • shell 变量路径)。安装相关的默认值。