Django自定义登录使用自己的模型 [英] Django custom login using own model
问题描述
班学生(models.Model):
first_name = models.CharField(**选项)
last_name = models.CharField(**选项)
username = models.EmailField(max_length = 100,unique = True)
password = models.CharField(_('password'),max_length = 128)
#学生模型的其他一些属性
类公司(models.Model):
compnay_name = models.CharField(**选项)
username = models.EmailField(max_length = 100,unique = True)
password = models.CharField(_('password'),max_length = 128)
#公司模型
我的要求:
- 学生和公司可以创建一个新的个人资料(提供注册表单)
- 为学生/公司创建新的个人资料,用户名,即电子邮件ID应该是唯一的。即电子邮件ID不应存在于学生&公司模式(任务完成)
- 创建了2张登录表单给学生&公司登录。
问题:
-
由于我没有使用或扩展用户模型,所以我无法使用django内置登录和验证方法。
-
如何编写自定义验证方法,该方法应该检查学生/公司用户名和密码。 (有2个不同的登录表单给学生公司)
请帮助我。 b
$ b
感谢您阅读我的查询。
backend.py
class LoginBackend:
def验证(self,username = None,password = None,model = None):
如果model ==Student:
lookup_model = Student
elif model ==Employer:
lookup_model = Employer
try:
user = lookup_model.objects.get(email = username)
除了异常,e:
返回无
返回用户
views.py
def check_auth(request):
user_object = Student.objects.get(email__iexact = unicode(email))
如果check_password(password,user_object.password):
print authenticate(
登录(request,user_object)
settings.py
AUTHENTICATION_BACKENDS =(proj.app.backends.LoginBackend,)
/ xxx / login / 中的AttributeError
'学生的对象没有属性'后端'
编写自定义身份验证后端。阅读:
[更新]
编写和注册自定义身份验证后端,您只需使用标准的Django身份验证模式。通过您的示例代码,我想你已经有所不同了。
由于电子邮件是您唯一的密钥,我建议使用电子邮件登录密钥,首先检查登录/密码对于一个模型,比其他模型。
从django.contrib.auth.models import User
class JayapalBackend(object )
def authenticate(self,username = None,password = None)
try:
o = Student.objects.get(email = username,password = password)
Student.DoesNotExist:
try:
o = Company.objects.get(email = username,password = password)
除了Company.DoesNotExist:
返回无
返回用户
def get_user(self,user_id):
try:
return User.objects.get(pk = user_id)
except User .DoesNotExist:
return无
然后只需使用标准d Django装饰器:
@login_required
def some_private_view(request):
...
I am using Django1.4 with PostgreSQL. I am developing an application in which I have two models i.e. Students, Company.
class students(models.Model):
first_name = models.CharField(**option)
last_name = models.CharField(**option)
username = models.EmailField(max_length=100, unique=True)
password = models.CharField(_('password'), max_length=128)
# Some other attributes for Student models
class company(models.Model):
compnay_name = models.CharField(**option)
username = models.EmailField(max_length=100, unique=True)
password = models.CharField(_('password'), max_length=128)
#Some other attributes for company models
My Requirement:
- Student and Company can create a new profile (provide a sign-up form)
- Which creating a new profile for Student/Company, username i.e. email id should be unique. i.e. Email id should not exist in Student & Company models.(task completed )
- Created 2 sign-In form for Student & Company login.
Issue:
As I am not using or extending User model, I am cannot use django in-built login & authenticate method.
How can I write a custom authentication method which should check user credentials in Student/Company username & password. (Have 2 different Sign-in form for Student & Company)
Please help me.
Thanks for reading my query.
backend.py
class LoginBackend:
def authenticate(self, username=None, password=None, model=None):
if model == "Student":
lookup_model = Student
elif model == "Employer":
lookup_model = Employer
try:
user = lookup_model.objects.get(email=username)
except Exception, e:
return None
return user
views.py
def check_auth(request):
user_object = Student.objects.get(email__iexact = unicode(email))
if check_password(password, user_object.password):
print authenticate(username = email, password = password, model = "Student")
login(request, user_object)
settings.py
AUTHENTICATION_BACKENDS = ("proj.app.backends.LoginBackend",)
Error
AttributeError at /xxx/login/
'Student' object has no attribute 'backend'
Write a custom authentication backend. Read this:
- Writing an authentication backend
- Handling authorization in custom backends
- settings.AUTHENTICATION_BACKENDS
[update]
Writing and registering a custom authentication backend, you just have to use the standard Django authentication patterns. By your sample code, I guess you have understood it differently.
Since email is your unique key, I suggest using email for the login key, first check the login/password against one Model, than the other.
from django.contrib.auth.models import User
class JayapalBackend(object):
def authenticate(self, username=None, password=None):
try:
o = Student.objects.get(email=username, password=password)
except Student.DoesNotExist:
try:
o = Company.objects.get(email=username, password=password)
except Company.DoesNotExist:
return None
return User.objects.get(email=o.email)
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Then just use standard Django decorators:
@login_required
def some_private_view(request):
...
这篇关于Django自定义登录使用自己的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!