首页 > 代码库 > login_required 登录验证

login_required 登录验证

登录视图(view.login)

def Login(request):
if request.method == ‘POST‘:
#username = request.POST.get(‘username‘)
     username = request.POST.get(‘email‘)
password = request.POST.get(‘password‘)
# print username, password
# user = userInfo.objects.filter(username=username, password=password)
user = auth.authenticate(username=username, password=password) #去auth_user表中查数据(默认)

# print type(user), user
‘‘‘
if len(user) == 1:
return redirect(‘/web/index/%s‘ % username)
request.session.set_expiry(60 * 30)
auth.login(request, user)
# return redirect(‘/web/index/‘)
else:
return render_to_response(‘login.html‘, {‘data‘: ‘用户名或密码错误‘,})
‘‘‘
if user is not None:

auth.login(request, user) #重要的两句
request.session.set_expiry(60 * 30)
# print ‘session expires at :‘,request.session.get_expiry_date()
return redirect(‘/web/index/%s‘ % username)
else:
return render_to_response(‘login.html‘, {‘data‘: ‘用户名或密码错误‘,})
else:
return render_to_response(‘login.html‘)




自建表绑定到auth_user
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
import django

class UserManager(BaseUserManager):
def create_user(self, email, name, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError(‘Users must have an email address‘)

user = self.model(
email=self.normalize_email(email),
name=name,
#token=token,
#department=department,
#tel=tel,
#memo=memo,

)

user.set_password(password)
user.save(using=self._db)
return user

def create_superuser(self, email, name ,password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(email,
password=password,
name=name,
#token=token,
#department=department,
#tel=tel,
#memo=memo,
)
user.is_admin = True
user.save(using=self._db)
return user


class UserProfile(AbstractBaseUser):
email = models.EmailField(
verbose_name=‘email address‘,
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)

name = models.CharField(max_length=32)
token = models.CharField(u‘token‘, max_length=128,default=None,blank=True,null=True)
department = models.CharField(u‘部门‘, max_length=32,default=None,blank=True,null=True)
#business_unit = models.ManyToManyField(BusinessUnit)
tel = models.CharField(u‘座机‘, max_length=32,default=None,blank=True,null=True)
mobile = models.CharField(u‘手机‘, max_length=32,default=None,blank=True,null=True)

memo = models.TextField(u‘备注‘, blank=True,null=True,default=None)
date_joined = models.DateTimeField(blank=True, auto_now_add=True)
#valid_begin = models.DateTimeField(blank=True, auto_now=True)
valid_begin_time = models.DateTimeField(default=django.utils.timezone.now)
valid_end_time = models.DateTimeField(blank=True,null=True)





USERNAME_FIELD = ‘email‘
#REQUIRED_FIELDS = [‘name‘,‘token‘,‘department‘,‘tel‘,‘mobile‘,‘memo‘]
REQUIRED_FIELDS = [‘name‘]

def get_full_name(self):
# The user is identified by their email address
return self.email

def get_short_name(self):
# The user is identified by their email address
return self.email

def __str__(self): # __unicode__ on Python 2
return self.email

def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_perms(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True

@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin

class Meta:
verbose_name = u‘用户信息‘
verbose_name_plural = u"用户信息"
def __unicode__(self):
return self.name

objects = UserManager()

参考:http://my.oschina.net/u/569730/blog/369144

login_required 登录验证