0

我有一个 django 应用程序并尝试在其中使用django-registration应用程序。以下是我的设置和代码

设置.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',
)

项目urls.py文件

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文件

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文件

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

因此,当用户点击 url 时,从上面的代码localhost:8000/accounts/register 中将显示一个注册表单,在输入所有字段(username,email, password, password)并单击注册按钮后,我得到以下信息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:
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: 

谁能知道如何避免这种情况NotImplementedError,实际上根据原始观点python2.7/site-packages/registration/views.pyregister方法是

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

但是我提供了用于在我的 url 中将用户注册为 {'backend': 'user_profile.backends.RegistrationBackend'},但它仍然没有覆盖和显示原始功能错误的后端类。

那么在上述情况下我做错了什么以及如何修复并使用户保存?

编辑

按照指示修改代码后Paco

当试图注册用户时,我得到了以下错误

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:
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
4

3 回答 3

4

我的类似问题解决了

from registration.backends.model_activation.views import RegistrationView

代替

from registration.views import RegistrationView

urls.py (django 1.9)

于 2016-06-24T06:17:19.057 回答
3

您不需要从引发 NotImplementedError 的方法继承。试试这个。

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

否则,你也会继承加薪。

您的编辑是一个不同的问题。

看看这个。这是由于 send_mail 功能。我引用:

出于调试目的,您可以使用以下命令设置本地 smtpserver:

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

并相应地调整您的邮件设置:

EMAIL_HOST = '本地主机' EMAIL_PORT = 1025

这在此处记录:测试电子邮件发送

于 2013-09-24T07:18:33.467 回答
1

如果你有 `[Errno 111] Connection denied',你必须有一个邮件服务器来发送电子邮件(也就是 postfix 或 sendmail)。启动您的邮件服务器!

于 2013-11-02T17:25:37.757 回答