我已经定义了一个自定义用户帐户,它利用了 auth User 模型中的几个内置项,并使用用户链接将这些与我在数据库上注册用户所需的一些附加字段链接起来。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
与我的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
....
....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(我希望我没有发布太多代码,只是想彻底)
如您所见,我最近进行了一些已注释掉的尝试。我尝试通过下载“registration”来使用RegistrationFormUniqueForm() 内置的你,但我实际上并不想制作新的注册表单,因为我已经有了一个工作表。
我继续尝试另一个建议,评论下的代码
# custom form to display additional sign up information
当我尝试使用已注册的电子邮件注册新用户时,它没有抛出任何错误并允许注册。我还尝试更改用户的电子邮件,它允许更改为已接收的电子邮件而没有异议。
任何人都可以建议一种方法来使用户注册保持对每个尝试使用可能已经被占用的电子邮件地址注册的人的唯一锁定吗?以及阻止他们将电子邮件更改为当前用户使用的电子邮件?
提前致谢。
编辑:我对模型注册表单 def clean_email() 和视图中的 def 进行了更改,以反映我目前拥有的仍然不起作用的内容。