我已经定义了一个自定义用户帐户,它使用auth用户模型中的几个内置程序,并使用用户链接将这些与我在数据库中注册用户所需的一些其他字段相关联。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
与我的models.py
相关# additional model to incorporate our custom fields to the auth user model
class Account(models.Model):
user = models.OneToOneField(User) #link (pointer) to the users other information in User model
birthdate = models.DateField(blank = True, ) # True makes this field optional
gender = models.CharField(max_length = 1, choices = GENDER_CHOICE, null = True, blank = True)
def __unicode__(self): # define a unicode for the user to access
return u'%s %s' % (self.user.first_name, self.user.last_name) # return first and last name in shell
# custom form to gather information from the user for a new account
class UserRegistration(UserCreationForm):
#class RegistrationForm(forms.ModelForm):
class Meta:
model = User
fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
# ensures uniqueness of user email addresses when registering
def clean_email(self):
print "In custom creation"
email = self.cleaned_data.get(email = 'email')
username = self.cleaned_data.get(username = 'username')
# checks if email address already exists
if User.objects.filter(email__iexact = self.cleaned_data['email']):
print "Email exists"
# if email and User.objects.filter(email__iexact = email).exclude(username=username).exists():
raise forms.ValidationError(u'Email Address is currently used by another user.')
return email
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
与views.py
相关def Main(request):
if request.user.is_authenticated():
latest_events = Event.objects.all().order_by('-created')[:10] # Returns latest 10 events
my_events = Event.objects.filter(creator=request.user)[:10] # Returns up to 10 events created by current User
my_calendars = Calendar.objects.filter(creator=request.user) # Returns all calendars created by the user
authForm = None
loginForm = None
headerType = "header.html"
else:
latest_events = None
my_events = None
my_calendars = None
headerType = "header_main.html"
authForm = UserRegistration(request.POST or None)
print "Creating account UserRegistration" # TESTING PRINT
print "User email = %s " %(User._meta.get_field('email'))
if request.method == 'POST':
if authForm.is_valid():
newUser = authForm.save(commit=False)
newUser.save()
newUser = authenticate(username=request.POST['username'], password=request.POST['password1'])
login(request, newUser)
return HttpResponseRedirect('/signup/')
....
....
more code on success redirection
....
....
~~~~~~~~~~~~~~~~~~~~~~~~~~~
(我希望我没有发布太多代码,只是想彻底)
正如你所看到的,我最近做了一些评论。我尝试通过下载“注册”来使用您在RegistrationFormUniqueForm()中内置的内容,但我实际上并不想创建一个新的注册表单,因为我已经有了一个工作表单。
我继续尝试另一个建议,即评论
下的代码# custom form to display additional sign up information
当我尝试使用已注册的电子邮件注册新用户时,它没有抛出任何错误并允许注册。我还尝试更改用户电子邮件,它允许更改已经收到的电子邮件,没有异议。
任何人都可以建议一种方法,使用户注册对每个试图注册可能已被采用的电子邮件地址的个人保持独特的锁定吗?以及阻止他们将电子邮件更改为当前用户使用的电子邮件?
提前致谢。
编辑:我对模型注册表单def clean_email()和def视图进行了更改,以反映我目前仍然无法正常工作的内容。
答案 0 :(得分:3)
对于两种表单,clean_email
方法的缩进都是错误的。目前,它们是Meta
类的方法,因此永远不会被调用。例如,注册表格应为:
class RegistrationForm(UserCreationForm):
#class RegistrationForm(forms.ModelForm):
class Meta:
model = User
fields = ("first_name", "last_name", "email", "username", "password1", "password2",)
def clean_email(self):
"""ensures uniqueness of user email addresses when registering"""
email = self.cleaned_data.get('email')
这可能不是真正的问题 - 将代码粘贴到堆栈溢出时很容易弄错缩进。如果是这样的话,我会删除答案。