注册用户时,在django注册中NotImplementedError [英] NotImplementedError in django-registration when registering a user

查看:458
本文介绍了注册用户时,在django注册中NotImplementedError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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',

项目 urls。 py 文件

  urlpatterns = patterns('',
url(r'^ admin /', include(admin.site.urls)),
url(r'^ accounts /',include('user_profile.urls')),
url(r'^ accounts /',include .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'),

pre>

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(s​​elf ,请求)
返回RegistrationForm

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

所以从上面的代码当用户点击url <$将显示c $ c> localhost:8000 / accounts / register
a注册表单,并输入所有字段后(用户名,电子邮件,密码,密码),并点击注册按钮我得到以下 NotImplementedError

  / accounts / register / 
中的NotImplementedError没有提供异常
请求方法:POST
请求URL:http:// localhost:8000 / accounts /注册/
Django版本:1.5.1
异常类型:NotImplementedError

追溯

 追溯:
文件/ home / user / proj / combined / local / lib / python2.7 / site-packages / django / core / handlers / base.pyin get_response
115. response = callback(request,* callback_args,** callback_kwargs)
文件/ home /用户/ PROJ /组合/本地/lib/python2.7/site-packages/django/views/generic/base.py查看
68. return self.dispatch(request,* args,** kwargs)
文件/ home / user / proj / combine / local / lib / python2.7 / site-packages / registration / views.py在dispatch
79. return super(RegistrationView,self).dispatch(request,* args,* * kwargs)
文件/home/user/proj/combined/local/lib/python2.7/site-packages/django/views/generic/base.py在dispatch
86. return handler (请求,* args,** kwargs)
文件/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py在帖子
35 。return self.form_valid(request,form)
文件/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.pyin form_valid
82 。new_user = self.register(request,** form.cleaned_data)
注册表中的/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.py文件
109. raise NotImplementedError

异常类型:/ accounts / register /
中的NotImplementedError异常值:

任何人都可以知道如何避免这个 NotImplementedError ,实际上是根据 python2.7 / site-packages / registration / views的原始视图。 py 注册方法是

  def register(self,request,** cleaning_data):

在这里实现用户注册逻辑。访问
请求和注册表单的完整的clean_data都可以使用



raise NotImplementedError

但是我提供用于在我的网址中注册用户的后端类作为{'backend':'user_profile.backends.RegistrationBackend'},但仍然不会覆盖并显示原始功能错误。



所以我在上面的情况下做错了什么,如何修复并使用户保存?



修改



修改如 Paco



当尝试注册用户我收到以下错误

 错误在/ accounts /注册/ 
[Errno 111]连接拒绝
请求方法:POST
请求URL:http:// localhost:8000 / accounts / register /
Django版本:1.5.1
异常类型:错误
异常值:
[Errno 111]连接拒绝

追溯

 追溯:
文件/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)
文件/home/user/proj/combined/local/lib/python2.7/site-packages/django /views / proj / combine / local / lib / lib / python2.7 / site-packages / registration / views.pyin dispatch
79. return super(RegistrationView,self).dispatch(request,* args,** kwargs)
文件/ home / user / proj / combined / local / lib / python2.7 / site-packages / django / views / generic / base.py在dispatch
86. return handler(request,* args,** kwargs)
文件/home/user/proj/combined/local/lib/python2.7/site-packages/registration/views.pyin post
35. return self.form_valid(request,form)
文件/ home / user / proj / combine / local / lib / python2 .7 / site-packages / registration / views.pyin form_valid
82. new_user = self.register(request,** form.cleaned_data)
文件/ home / user / proj / combined /寄存器
中的本地/ lib / python2.7 / site-packages / registration / backends / default / views.py)80.密码,站点)
文件/ home / user / proj / combined / local内部
中的/lib/python2.7/site-packages/django/db/transaction.py返回func(* args,** kwargs)
文件/ home / user / proj / create_inactive_user
中的组合/本地/ lib / python2.7 / site-packages / registration / models.py 91. registration_profile.send_activation_email(site)
文件/ home / user / proj / combined / local /lib/python2.7/site-packages/registration/models.pyin send_activation_email
270. self.user.email_user(subject,message,settings.DEFAULT_FROM_EMAIL)
文件/ home / user / PROJ /组合/ local / lib目录/ python2.7 /站点包/ Django的/的contrib / auth /中型号.pyin email_user
422. send_mail(subject,message,from_email,[self.email])
文件/home/user/proj/combined/local/lib/python2.7/site- package / django / core / mail / __ init__.pyin send_mail
62. connection = connection).send()
文件/home/user/proj/combined/local/lib/python2.7发送
中的/site-packages/django/core/mail/message.py返回self.get_connection(fail_silently).send_messages([self])
文件/ home / user / proj / send_messages中的组合/本地/ lib / python2.7 / site-packages / django / core / mail / backends / smtp.py
88. new_conn_created = self.open()
文件/ home / user / proj / combine / local / lib / python2.7 / site-packages / django / core / mail / backends / smtp.py在
中。文件/usr/lib/python2.7/smtplib.pyin __init__
249.(code,msg)= self.connect(host,port)
文件/ usr / lib / python2 7 / smtplib.py在
309. self.sock = self._get_socket(host,port,self.timeout)
文件/usr/lib/python2.7/smtplib.py在_get_socket
284 。return socket.create_connection((port,host),timeout)
在create_connection中的文件/usr/lib/python2.7/socket.py
571. raise err

异常类型:/ accounts / register /
中的错误异常值:[Errno 111]连接拒绝


解决方案

您不需要继承自引发NotImplementedError的方法。

  def register(self,request,** kwargs):
UserProfile.objects.create ** kwargs)
返回用户

否则,你继承加注。 p>

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



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


为了进行调试,您可以使用此
命令设置本地smtpserver:



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



并相应调整邮件设置:



EMAIL_HOST ='localhost'
EMAIL_PORT = 1025



这里记录了:测试电子邮件发送



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

解决方案

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

这篇关于注册用户时,在django注册中NotImplementedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆