我在 Django 中有一个简单的表单 login.html。我从登录表单中获取用户名和密码,并用于在我的 Django 视图中进行验证:
def login_upload(request):
if request.method == 'POST':
username = request.POST['username']
username1 = ''.join(username)
password = request.POST['password']
password = ''.join(password)
password1 = hashlib.sha1(password).hexdigest()
user = authenticate(username=username1, password=password1)
user_id = request.galaxyuser.id
if len(user) > 0:
return render_to_response('right.html', {'user_id':user_id}, context_instance=RequestContext(request))
else:
return render_to_response('wrong.html', context_instance=RequestContext(request))
else:
return render_to_response('login.html', context_instance=RequestContext(request))
我还想获取稍后必须使用的用户 ID。我尝试使用:
user_id = request.galaxyuser.id
但它返回None
值。这是我的models.py:
class GalaxyUser(models.Model):
id = models.IntegerField(primary_key=True)
create_time = models.DateTimeField(null=True, blank=True)
update_time = models.DateTimeField(null=True, blank=True)
email = models.CharField(max_length=765)
password = models.CharField(max_length=120)
external = models.IntegerField(null=True, blank=True)
deleted = models.IntegerField(null=True, blank=True)
purged = models.IntegerField(null=True, blank=True)
username = models.CharField(max_length=765, blank=True)
form_values_id = models.IntegerField(null=True, blank=True)
disk_usage = models.DecimalField(null=True, max_digits=16, decimal_places=0, blank=True)
class Meta:
db_table = u'galaxy_user'
编辑: 我的自定义身份验证功能:
def authenticate(username='username', password='password'):
return GalaxyUser.objects.filter(username=username, password=password)