添加简单的而外的profile!..>
1.django扩展
首先,对于添加profile , 可以使用扩展 django-registration 和 django-profile, 后者的使用依赖于前者。
我没用django-profile ...
2.官方方案
官方文档还是比较难看懂的,反正我看了半天,他说 AUTH_PROFILE_MODULE 在1.5 过时了,但是接下来有大半的内容在 AUTH_PROFILE_MODULE,我都不懂他想干嘛,粽子,他提到了两种方法,我用的是第一种方法,使用model里面的OneToOneFiled
3.正文
我们把额外的信息放在 UserProfile 模型里面,然后一对一的指向User 就好了!这样唯一要作的就是注册时我们要使用两个表单。
首先建立Profile 在model.py 中。
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
def __str__(self):
return self.studentid
user = models.OnToOneField(User)
name = models.CharField(max_lenght='5')
studentid = models.DecimalField(max_digits=10,decimal_places=10)
建立两个表单,
from django.forms import ModelForm
from django.contrib.auth.forms import UserCreationForm
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields=('name','studentid')
class UserForm(UserCreationForm):
pass
之后就是验证着两个表单。
def Register(request):
if request.method == "POST":
uf=UserForm(request,prefix='user')
upf=UserProfileForm(request,prefix='profile')
if uf.is_valid()*upf.is_valid() :
user=uf.save()
profile=upf.save(commit=False)
profile.user=user
profile.save()
return HttpResponseRedirect(reverse('thanks'))
else :
uf=UserForm(prefix='user')
upf=UserProfileForm(prefix='profile')
return render(request,'register.html',{
'userform':uf,
'profileform':upf,
})
大致就是这样。