Django注册:作为用户名发送电子邮件
如何使用电子邮件而不是用户名进行身份验证 此外,一旦我已经安装了注册模块,我将如何实施此更改Django注册:作为用户名发送电子邮件,django,django-registration,Django,Django Registration,如何使用电子邮件而不是用户名进行身份验证 此外,一旦我已经安装了注册模块,我将如何实施此更改 我是否在lib文件夹中编辑此解决方案 亲爱的Django编码员同事 我认为这是最好的办法。祝你好运 第一步是创建要使用的表单 project/accounts/forms.py from django import forms from registration.forms import RegistrationForm from django.contrib.auth.models import Us
我是否在lib文件夹中编辑此解决方案 亲爱的Django编码员同事 我认为这是最好的办法。祝你好运 第一步是创建要使用的表单 project/accounts/forms.py
from django import forms
from registration.forms import RegistrationForm
from django.contrib.auth.models import User
class Email(forms.EmailField):
def clean(self, value):
super(Email, self).clean(value)
try:
User.objects.get(email=value)
raise forms.ValidationError("This email is already registered. Use the 'forgot password' link on the login page")
except User.DoesNotExist:
return value
class UserRegistrationForm(forms.Form):
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
#email will be become username
email = Email()
def clean_password(self):
if self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends import default
class Backend(default.DefaultBackend):
def register(self, request, **kwargs):
email, password = kwargs['email'], kwargs['password1']
username = email
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
(r'^accounts/', include('registration.backends.default.urls')),
from django.conf.urls.defaults import *
from registration.backends.default.urls import *
from accounts.forms import UserRegistrationForm
urlpatterns += patterns('',
#customize user registration form
url(r'^register/$', 'registration.views.register',
{
'backend': 'accounts.regbackend.Backend',
'form_class' : UserRegistrationForm
},
name='registration_register'
),
)
在这里,您将创建一个文件来覆盖django注册中的register()函数
project/accounts/regbackend.py
from django import forms
from registration.forms import RegistrationForm
from django.contrib.auth.models import User
class Email(forms.EmailField):
def clean(self, value):
super(Email, self).clean(value)
try:
User.objects.get(email=value)
raise forms.ValidationError("This email is already registered. Use the 'forgot password' link on the login page")
except User.DoesNotExist:
return value
class UserRegistrationForm(forms.Form):
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
#email will be become username
email = Email()
def clean_password(self):
if self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends import default
class Backend(default.DefaultBackend):
def register(self, request, **kwargs):
email, password = kwargs['email'], kwargs['password1']
username = email
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
(r'^accounts/', include('registration.backends.default.urls')),
from django.conf.urls.defaults import *
from registration.backends.default.urls import *
from accounts.forms import UserRegistrationForm
urlpatterns += patterns('',
#customize user registration form
url(r'^register/$', 'registration.views.register',
{
'backend': 'accounts.regbackend.Backend',
'form_class' : UserRegistrationForm
},
name='registration_register'
),
)
将URL指向要使用的路径
project/url.py
from django import forms
from registration.forms import RegistrationForm
from django.contrib.auth.models import User
class Email(forms.EmailField):
def clean(self, value):
super(Email, self).clean(value)
try:
User.objects.get(email=value)
raise forms.ValidationError("This email is already registered. Use the 'forgot password' link on the login page")
except User.DoesNotExist:
return value
class UserRegistrationForm(forms.Form):
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
#email will be become username
email = Email()
def clean_password(self):
if self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends import default
class Backend(default.DefaultBackend):
def register(self, request, **kwargs):
email, password = kwargs['email'], kwargs['password1']
username = email
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
(r'^accounts/', include('registration.backends.default.urls')),
from django.conf.urls.defaults import *
from registration.backends.default.urls import *
from accounts.forms import UserRegistrationForm
urlpatterns += patterns('',
#customize user registration form
url(r'^register/$', 'registration.views.register',
{
'backend': 'accounts.regbackend.Backend',
'form_class' : UserRegistrationForm
},
name='registration_register'
),
)
告诉URL使用注册视图的自定义后端。另外,导入您创建的表单并将其添加到要由视图处理的url中
project/accounts/url.py
from django import forms
from registration.forms import RegistrationForm
from django.contrib.auth.models import User
class Email(forms.EmailField):
def clean(self, value):
super(Email, self).clean(value)
try:
User.objects.get(email=value)
raise forms.ValidationError("This email is already registered. Use the 'forgot password' link on the login page")
except User.DoesNotExist:
return value
class UserRegistrationForm(forms.Form):
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
#email will be become username
email = Email()
def clean_password(self):
if self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']
from django.conf import settings
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
from registration import signals
from registration.forms import RegistrationForm
from registration.models import RegistrationProfile
from registration.backends import default
class Backend(default.DefaultBackend):
def register(self, request, **kwargs):
email, password = kwargs['email'], kwargs['password1']
username = email
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
(r'^accounts/', include('registration.backends.default.urls')),
from django.conf.urls.defaults import *
from registration.backends.default.urls import *
from accounts.forms import UserRegistrationForm
urlpatterns += patterns('',
#customize user registration form
url(r'^register/$', 'registration.views.register',
{
'backend': 'accounts.regbackend.Backend',
'form_class' : UserRegistrationForm
},
name='registration_register'
),
)
希望能成功
-Matt对于Django>=1.3和<1.5,在github上有 最近我使用注册,我很容易切换到。它在Django 1.4上运行良好,重用相同的模板,只需添加
REGISTRATION\u EMAIL\u ACTIVATE\u SUCCESS\u URL=“/profiles/create/”
重定向到创建页面
Django 1.5通过自定义用户模型克服了这个问题。如Django 1.5文件所述:
描述用户模型上用作唯一标识符的字段名称的字符串。这通常是某种用户名,但也可以是电子邮件地址或任何其他唯一标识符
最简单的方法是使用CSS display:none属性隐藏注册表表单模板中的username字段。并添加一个脚本,该脚本从电子邮件表单字段获取值,并在隐藏用户名字段中设置值。因此,在提交表单时,用户会在不知情的情况下以用户名的形式提交电子邮件。唯一的问题是,默认情况下,用户名字段不能接受长度超过30个字符的电子邮件。因此,为了安全起见,您应该在数据库中手动增加用户名长度 以下是新的registration_form.html模板:
{%csrf_令牌%}
用户名:
{%if form.username.errors%}
{{form.username.errors.as_text}
{%endif%}
{{form.username}
电邮地址:
{%if form.email.errors%}
{{form.email.errors.as_text}
{%endif%}
{{form.email}
密码:
{%if form.password1.errors%}
{{form.password1.errors.as_text}
{%endif%}
{{form.password1}}
密码(再次键入以捕捉输入错误):
{%if form.password2.errors%}
{{form.password2.errors.as_text}
{%endif%}
{{form.password2}}
我试过了,效果很好,但最后还是用了谢谢你的输入!希望我将来会使用它,其他人也可以使用它@Eva611-我很高兴你能理解它。默认情况下,用户名字段限制为30个字符;如果您试图使用电子邮件地址作为用户名,这可能会导致一些问题。除上述步骤外,更改username列以接受更长的字符串。例如,在Postgres中,查询将是ALTERTABLE auth_user ALTER COLUMN username TYPE varchar(75)
我认为应该是clean_password2,而不是UserRegistrationForm中的clean_password和clean_data,而不是data.FWIW现代django的用户名最大长度现在似乎默认为150 max。