注册后,我想请求用户:
我在StackOverflow上读过很多类似的情况。在模型.py,我这样扩展用户模型:
# models.py
class UserProfile(models.Model):
company = models.CharField(max_length = 50)
user = models.OneToOneField(User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
来源:Extending the User model with custom fields in Django
我还补充道:
^{pr2}$在视图.py公司名称:
# views.py
@csrf_exempt
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
new_user = form.save()
first_name, last_name = request.POST['fullname'].split()
email = request.POST['email']
company = request.POST['company'],
new_user = authenticate(
username = email,
password = request.POST['password']
)
# Log the user in automatically.
login(request, new_user)
现在,它不存储公司名称。我该怎么做?在
别忘了在设置中配置UserProfile类,这样Django就知道返回什么了user.get_配置文件()
相关问题 更多 >
编程相关推荐