Django休息口令扩展注册表 [英] Django rest auth extend registration form

查看:149
本文介绍了Django休息口令扩展注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django-rest-auth进行身份验证和注册。我正在使用电子邮件登录用户密码。 我想在注册时存储用户的名字和姓氏 (也许更多的字段),即在客户端注册表单上将有以下字段(电子邮件,密码,名字,姓氏等)。目前的注册表只有电子邮件,密码1和密码2字段,注册基本成功。

I am using Django-rest-auth for authentication and registration. I am using email to loging in the user with password. I want to store user's first name and last name at the time of registration (perhaps few more fields) i.e. On client side on registration form will have following fields(email, password, first name, last name, etc..). Currently registration form has only email, password 1 and password 2 fields and registration is basically successful.

我的settings.py具有以下功能:

My settings.py has following:

#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",

#`allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend", 
)

我该如何实现?

推荐答案

如果要扩展注册表单,您将需要创建自己的具有更多字段的自定义用户。

If you want to extend the registration form, you're going to need to create your own custom user that has more fields.

所以在你的模型文件中有这个

So in your models file have this

from django.contrib.auth.models import (
   AbstractBaseUser, PermissionsMixin,
)

class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    first_name = models.TextField(default='')
    last_name = models.TextField(default='')
    #any other fields you want

    USERNAME_FIELD = 'email'

然后在您想要的设置中

AUTH_USER_MODEL = 'app_name.CustomUser'

然后django休息应该使用此用户模型,当您访问此表单页面

and then django rest should use this user model as when you visit this form page

我希望这有帮助!

编辑

我相信这应该会自动处理密码,因为它扩展了AbtractBaseUser,让我知道如果不是

I believe this should automatically handle passwords since it extends AbtractBaseUser, let me know if it doesn't

这篇关于Django休息口令扩展注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆