[英]After extending the User model in django, I can not log in
That's how I"ve extended User model: 这就是我扩展用户模型的方式:
class User(AbstractBaseUser):
email = models.EmailField(verbose_name='email',max_length=255,unique=True, db_index=True,)
username = models.CharField(verbose_name='username', max_length=255, unique=True)
first_name = models.CharField(verbose_name='first_name', max_length=255, blank=True)
last_name = models.CharField(verbose_name='last_name', max_length=255, blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
avatar = models.ImageField(upload_to='profile_images', blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['username']
def get_full_name(self):
return '%s %s' % (self.first_name, self.last_name,)
def get_short_name(self):
return self.username
def __unicode__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
It works good , but after new user registration I can't log in with his username and password and in my database I have now 2 tables with users 效果很好,但是在注册新用户后,我无法使用他的用户名和密码登录,并且在我的数据库中,我现在有2个用户表
auth_user - old users auth_user-旧用户
blog_user - new extended users blog_user-新的扩展用户
And I can log in only with users from auth_user . 而且我只能使用auth_user中的用户登录 。
That's what I have for login in views.py: 这就是我在views.py中登录的内容:
@csrf_protect
def loginn(request):
c = {}
c.update(csrf(request))
return render_to_response("login/login.html", c)
@csrf_protect
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render_to_response('login/loggedin.html',RequestContext(request))
else:
return HttpResponseRedirect('/posts/invalid')
So , how can I log in with user from my new , extended table ? 那么,如何从新的扩展表中以用户身份登录?
In order to allow a custom User model to be used within the authenticate()
method, Django requires setting the AUTH_USER_MODEL
setting. 为了允许在authenticate()
方法中使用自定义用户模型,Django需要设置AUTH_USER_MODEL
设置。 https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model
For your example code it would be AUTH_USER_MODEL = 'blog.User'
within settings.py 对于您的示例代码,它是settings.py中的AUTH_USER_MODEL = 'blog.User'
The authenticate()
method makes a call to get_user_model()
which goes and finds if the AUTH_USER_MODEL setting is set, and then uses that as it's base for the model which to authenticate against. authenticate()
方法调用get_user_model()
并查找是否设置了AUTH_USER_MODEL设置,然后将其用作进行验证的模型的基础。
Please be sure to read the documentation on substituting a custom user model within Django. 请确保阅读有关在Django中替换自定义用户模型的文档。 There are a handful of caveats that you should be aware of before making a decision to switch the auth user model. 在决定切换auth用户模型之前,应注意一些警告。
If it's possible, I would recommend just extending the base auth user model to include any application specific fields you would need because it appears you are still logging users in via Username. 如果可能的话,我建议您仅扩展基本身份验证用户模型,使其包含您需要的任何特定于应用程序的字段,因为看起来您仍在通过用户名登录用户。 https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.