Django用户身份验证不起作用 [英] Django user authentication not working

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

问题描述

我正在使用来自django的默认模型User(因为我正在使用django-facebook),并且用户身份验证在模板中不起作用.这是代码:

I'm using the default model User from django (since I'm using django-facebook) and the user authentication does not work in the template. Here is the code:

{% if request.user.is_authenticated %}
   Welcome, {{ request.user.first_name }}
   <a href="/logout">logout</a>
{% else %}
   <form action="{% url facebook_connect %}?facebook_login=1" method="post">
        <a href="javascript:void(0);" style="font-size: 20px;" onclick="F.connect(this.parentNode);">
             <img src="/media/images/site/facebook_login.png" alt="facebook_login" class="facebook_login_button"/>
        </a>
        <input type="hidden" value="{{ request.path }}" name="next" />
   </form>
{% endif %}

我已经从视图传递了context_instance:

I've passed the context_instance from the view:

return render_to_response('index.html', context, context_instance=RequestContext(request))

但是仍然无法正常工作.感谢您的帮助!

But still it won't work. Thanks for your help!

用户成功使用django-facebook登录(已插入auth_user表),并显示了他的数据,但是当用户注销时,他的数据消失了,并且不显示上面编辑后的代码中的登录表单!

The user successfully logs in with django-facebook (inserted into auth_user table) and his data is shown but when the user logs out his data disappears and the login form as seen on the edited code above is not show!

这是我用于登录和注销用户的完整视图:

Here is the complete view I use for logging in and out the user:

def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')

def signin(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            # TO RETURN ERROR
            return HttpResponseRedirect('/')
    else:
        # TO RETURN ERROR
        return HttpResponseRedirect('/')

如果我从request.user.first_name中删除请求,则会出现错误:

If I remove the request from request.user.first_name an error appears:

您需要在项目设置中设置AUTH_PROFILE_MODULE

You need to set AUTH_PROFILE_MODULE in your project settings

那是我可以获得的所有详细信息.

That is about all the details I can get..

推荐答案

我不知道request.user.is_authenticated无法正常工作的原因,但是我找到了解决方法-使用

I am not aware of the reason why request.user.is_authenticated is not working but I have found a solution - Using

{% if not user.is_anonymous %}

代替

{% if request.user.is_authenticated %}

似乎可以解决问题.如果有效并且仅登录的用户会看到他们的信息和退出"按钮.

seems to do the trick. The if works and only logged in users are shown their info and the log out button.

这篇关于Django用户身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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