如何从Django的表单或数据库中获取用户ID? [英] How to take user id from the form or database in Django?

查看:146
本文介绍了如何从Django的表单或数据库中获取用户ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个简单的形式login.html.我从登录表单中获取用户名和密码,并用于在Django视图中进行验证:

I have a simple form login.html in Django. I take username and password from the login form and use to validate in my Django view:

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.我尝试使用:

I also want to get the id of the user which I have to use later. I tried using:

user_id = request.galaxyuser.id

但是它返回 None 值.这是我的models.py:

But it returns None value. Here's my 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'

修改:我的自定义身份验证功能:

My custom authentication function:

def authenticate(username='username', password='password'):
    return GalaxyUser.objects.filter(username=username, password=password)

推荐答案

我不知道 request.galaxyuser 应该是什么,但是给定用户对象,则id是该对象的一个​​属性.目的.如果凭据有效,则 authenticate 函数应该返回一个User对象.

I have no idea what request.galaxyuser is supposed to be, but given a user object, the id is an attribute of that object. The authenticate function is supposed to return a User object if the credentials are valid.

user = authenticate(username=username1, password=password1)
user_id = request.galaxyuser.id  # what are you doing here?

user_id = user.id  # this is the user ID.

根据您实现自定义 User 类的方式,此问题可能会有不同的答案.我假设这是 authenticate 返回的类.

This question might have different answers depending on how you implemented your custom User class. I'm assuming it's the class returned by authenticate.

您的示例显示您返回了一个 QuerySet (一个 .filter ),它是行的列表.

Your example shows you returning a QuerySet (a .filter) which is a list of rows.

您需要返回一个对象,以便可以查找ID属性,或者需要修改视图以解决这种奇怪的行为.身份验证应该返回经过身份验证的用户对象,所以我将使用前者.

You need to either return one object so that you can do look up the ID attribute, or you need to modify the view to account for this strange behavior. Authenticate is supposed to return the authenticated user object, so I woul ddo the former.

def authenticate(.....):
    try:
        return GalaxyUser.objects.get(username=username, password=password)
    except GalaxyUser.DoesNotExist:
        return None

user = authenticate(...)
user_id = user.id

这篇关于如何从Django的表单或数据库中获取用户ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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