NotImplementedError in django-registration when re

2020-03-31 18:22发布

问题:

I have a django app and trying to use django-registration app in it. And below are my settings and codes

settings.py

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'south',
    'registration',
    'user_profile',
 )

AUTH_PROFILE_MODULE = "user_profile.UserProfile"
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)

project urls.py file

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('user_profile.urls')),
    url(r'^accounts/', include('registration.backends.default.urls')),
 )

user_profile urls.py file

from registration.views import RegistrationView

urlpatterns = patterns('',
    url(r'^register/$', RegistrationView.as_view(),
        {'backend': 'user_profile.backends.RegistrationBackend'},
        name='registration_register'),
)

user_profile.backends.py file

from .forms import RegistrationForm
from user_profile.models import UserProfile
from registration.views import RegistrationView

class RegistrationBackend(RegistrationView):
    def get_form_class(self, request)
        return RegistrationForm

    def register(self, request, **kwargs):
        kwargs["username"] = kwargs["email"]
        user = super(RegistrationBackend, self).register(request, **kwargs)
        UserProfile.objects.create(user=user)
        return user

So from the above code when the user hits the url localhost:8000/accounts/register a registration form will be displayed, and after entering all the fields(username,email, password, password) and clicked on register button i am getting the below NotImplementedError

NotImplementedError at /accounts/register/
No exception supplied
Request Method: POST
Request URL:    http://localhost:8000/accounts/register/
Django Version: 1.5.1
Exception Type: NotImplementedError

Traceback

Traceback:
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in dispatch
  79.         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in post
  35.             return self.form_valid(request, form)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in form_valid
  82.         new_user = self.register(request, **form.cleaned_data)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in register
  109.         raise NotImplementedError

Exception Type: NotImplementedError at /accounts/register/
Exception Value: 

can anyone know how to avoid this NotImplementedError, actually according to the original view at python2.7/site-packages/registration/views.py , the register method is

def register(self, request, **cleaned_data):
    """
    Implement user-registration logic here. Access to both the
    request and the full cleaned_data of the registration form is
    available here.

    """
    raise NotImplementedError

But i am providing the backend class to use for registering the user in my url as {'backend': 'user_profile.backends.RegistrationBackend'}, but still it is not overriding and displaying the origin functional error.

So what i am doing wrong in the above scenario and how to fix and make the user save ?

Edit

After modifying code as indicated by Paco

when tried to register the user i got below error

error at /accounts/register/
[Errno 111] Connection refused
Request Method: POST
Request URL:    http://localhost:8000/accounts/register/
Django Version: 1.5.1
Exception Type: error
Exception Value:    
[Errno 111] Connection refused

Traceback

Traceback:
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in dispatch
  79.         return super(RegistrationView, self).dispatch(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in post
  35.             return self.form_valid(request, form)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py" in form_valid
  82.         new_user = self.register(request, **form.cleaned_data)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/backends/default/views.py" in register
  80.                                                                     password, site)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
  223.                 return func(*args, **kwargs)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/models.py" in create_inactive_user
  91.             registration_profile.send_activation_email(site)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/registration/models.py" in send_activation_email
  270.         self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/contrib/auth/models.py" in email_user
  422.         send_mail(subject, message, from_email, [self.email])
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/mail/__init__.py" in send_mail
  62.                         connection=connection).send()
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/mail/message.py" in send
  255.         return self.get_connection(fail_silently).send_messages([self])
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in send_messages
  88.             new_conn_created = self.open()
File "/home/user/proj/combined/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py" in open
  49.                                            local_hostname=DNS_NAME.get_fqdn())
File "/usr/lib/python2.7/smtplib.py" in __init__
  249.             (code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py" in connect
  309.         self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py" in _get_socket
  284.         return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py" in create_connection
  571.         raise err

Exception Type: error at /accounts/register/
Exception Value: [Errno 111] Connection refused

回答1:

You don't need to inherit from a method that raises NotImplementedError. Try this instead.

def register(self, request, **kwargs):
    UserProfile.objects.create(**kwargs)
    return user

Otherwise, you inheriting the raise as well.

Your edit is a different issue.

Have a look at this. It is due to the send_mail function. I quote:

For debugging purposes you could setup a local smtpserver with this command:

python -m smtpd -n -c DebuggingServer localhost:1025

and adjust your mail settings accordingly:

EMAIL_HOST = 'localhost' EMAIL_PORT = 1025

This is documented here: Testing e-mail sending



回答2:

If you have `[Errno 111] Connection refused', you must have a mail server to sen thje email (Aka postfix or sendmail). Start your mail server !



回答3:

My similar problem solved by

from registration.backends.model_activation.views import RegistrationView

instead of

from registration.views import RegistrationView

in urls.py (django 1.9)