Django以匿名用户身份登录? [英] Django logging in as anonymous user?

查看:955
本文介绍了Django以匿名用户身份登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录用户。创建他的个人资料后。
,但是当他授权他时,他以匿名用户身份登录。

 从django.contrib.auth导入验证作为授权

def shopify_register(request):

print############ ***** GOT DETAILS ***** ## ##########
password = request.POST ['pass']


subs,stat = Subscription.objects.get_or_create(validity = datetime .now()+ timedelta(days = 30))
newuser,stat = User.objects.get_or_create(username = request.POST ['email'])
newuser.set_password(password)
newuser.save()
user,stat = UserProfile.objects.get_or_create(subscription = subs,user = newuser)
domain,stat = Domain.objects.get_or_create(user_profile = user,shortname = request。 POST ['shop_name'],keywords = request.POST ['keywords']

profile,stat = ShopifyUserProfile.objects.get_or_create(shop_user = user .. etc)
username =请求.POST ['email']



authuser = authorize(use rname = username,password = mdpass)
domain = Domain.objects.get(user_profile = user)
testimonies = Testimony.objects.filter(domain = domain).filter(show = 0).order_by 'timestamp')
c = RequestContext(request,{
'user':user,
'testimonies':testimonies,
'request':request,
'提到':1,
'domain':domain
})
return render_to_response('homepage.html',context_instance = c)
pre>

更新:

  authuser = authorize(username = username, password = mdpass)

这不授权用户。虽然用户已创建。



它工作正常,但它将用户记录为匿名用户?这里有什么问题?



删除了md5哈希,但用户仍未被授权。



更新:2

  ipdb> authuser = authorize(username = username,password = password)
ipdb> authuser
< User:ratan@kumar.com>
ipdb> temp = auth_login(request,authuser)
ipdb> temp
ipdb>打印temp

这意味着它已经授权用户,但无法登录他在。

解决方案

您需要在验证后调用login功能。

 从django.contrib.auth导入登录为auth_login 
auth_login(请求,用户)


I am trying to login a user . after creating his profile . but when he authorises him he logs in as anonymous user.

from django.contrib.auth import authenticate as authorise

def shopify_register(request):

    print "############*****GOT DETAILS*****############"
    password = request.POST['pass']


    subs, stat = Subscription.objects.get_or_create(validity=datetime.now()+timedelta(days=30))
    newuser, stat = User.objects.get_or_create(username=request.POST['email'])
    newuser.set_password(password)
    newuser.save()
    user, stat = UserProfile.objects.get_or_create(subscription=subs,user=newuser)
    domain, stat = Domain.objects.get_or_create(user_profile=user, shortname=request.POST['shop_name'], keywords=request.POST['keywords'])

    profile,stat = ShopifyUserProfile.objects.get_or_create(shop_user =user.. etc )
    username = request.POST['email']



    authuser = authorise(username=username, password=mdpass)
    domain = Domain.objects.get(user_profile=user)
    testimonies = Testimony.objects.filter(domain=domain).filter(show=0).order_by('-timestamp')
    c = RequestContext(request, {
        'user' : user,
        'testimonies': testimonies,
        'request': request,
        'mentions': 1,
        'domain': domain
    })
    return render_to_response('homepage.html', context_instance=c)

UPDATE :

authuser = authorise(username=username, password=mdpass)

this does not authorises the user . though the User has been created .

It works fine but it logs user as anonymous user ? what is wrong here ?

Removed the md5 hashing but still user is not being authorised.

UPDATE :2

ipdb> authuser = authorise(username=username, password=password)
ipdb> authuser
<User: ratan@kumar.com>
ipdb> temp=auth_login(request,authuser)
ipdb> temp
ipdb> print temp
None

It means it has authorised the user but it has not able to log him in.

解决方案

You need to call login function after authenticate.

from django.contrib.auth import login as auth_login
auth_login(request, user)

这篇关于Django以匿名用户身份登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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