具有自定义用户模型的django-allauth引发错误

时间:2018-06-30 08:25:18

标签: django django-rest-framework django-allauth

我使用django restframework和allauth进行简单注册。

我还定制了所需的用户模型。

[settings.py]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users', # Cutom User model
    'rest_framework',
    'rest_auth',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'rest_auth.registration',
]

# Configure the JWTs to expire after 1 hour, and allow users to refresh near-expiration tokens
JWT_AUTH = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(hours=1),
    'JWT_ALLOW_REFRESH': True,
}

# Make JWT Auth the default authentication mechanism for Django
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

# Enables django-rest-auth to use JWT tokens instead of regular tokens.
REST_USE_JWT = True

AUTH_USER_MODEL = 'users.user'
SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'email'

[/ users / models.py]

from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser, PermissionsMixin
)
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

class UserManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError(_('Users must have an email address'))

        user = self.model(
            email=self.normalize_email(email),
            username=username,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(
            email=email,
            password=password,
            username=username,
        )

        user.is_superuser = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(
        verbose_name=_('Email address'),
        max_length=255,
        unique=True,
    )
    username = models.CharField(
        verbose_name=_('username'),
        max_length=30,
        unique=True
    )
    is_active = models.BooleanField(
        verbose_name=_('Is active'),
        default=True
    )
    date_joined = models.DateTimeField(
        verbose_name=_('Date joined'),
        default=timezone.now
    )
    salt = models.CharField(
        verbose_name=_('Salt'),
        max_length=10,
        blank=True
    )

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', ]

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
        ordering = ('-date_joined',)

    def __str__(self):
        return self.username

    def get_full_name(self):        
        return self.username

    def get_short_name(self):
        return self.username

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All superusers are staff
        return self.is_superuser

    get_full_name.short_description = _('Full name')

[urls.py]

from django.contrib import admin
from django.urls import path, include
from rest_framework_jwt.views import refresh_jwt_token

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('rest_auth.urls')),
    path('registration/', include('rest_auth.registration.urls')),
    path('refresh-token/', refresh_jwt_token),
]

看起来一切都很好。

但是当我这样注册用户时->

url:/注册 参数:username=test&password1=testtest11&password2=testtest11&email=test@gmail.com

它引发错误。

Internal Server Error: /registration/
Traceback (most recent call last):
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_auth/registration/views.py", line 46, in dispatch
    return super(RegisterView, self).dispatch(*args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_framework/generics.py", line 192, in post
    return self.create(request, *args, **kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_auth/registration/views.py", line 65, in create
    user = self.perform_create(serializer)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/rest_auth/registration/views.py", line 81, in perform_create
    None)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/utils.py", line 183, in complete_signup
    signal_kwargs=signal_kwargs)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/utils.py", line 143, in perform_login
    send_email_confirmation(request, user, signup=signup)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/utils.py", line 314, in send_email_confirmation
    signup=signup)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/models.py", line 60, in send_confirmation
    confirmation.send(request, signup=signup)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/models.py", line 166, in send
    get_adapter(request).send_confirmation_mail(request, self, signup)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/adapter.py", line 444, in send_confirmation_mail
    ctx)
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/allauth/account/adapter.py", line 137, in send_mail
    msg.send()
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/mail/message.py", line 294, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
    new_conn_created = self.open()
  File "/Users/hide/.local/share/virtualenvs/test-OJ76OynC/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 63, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

(奇怪的是,寄存器可以正常工作,但是会抛出错误)

没有详细的解释,所以我很困惑如何解决它。

这里有解决方案吗?

谢谢!

0 个答案:

没有答案