Django 模板检查用户是否已通过身份验证并且存在

发布于 2024-11-05 15:26:53 字数 2154 浏览 0 评论 0原文

我正在创建用户登录并将 signin 链接更改为 signout

我的views.py是:

#This index is for my landing page i.e.first.html where I have Sigin link which needs to hidden on Successful
def index(request):
    if 'username' not in request.session :
        return render_to_response('gharnivas/ghar.html', context_instance=RequestContext(request))
    else:
    u = request.session['username']
    return render_to_response('gharnivas/ghar.html',{ 'user' : u },context_instance=RequestContext(request))


#This here, is the view for my check the user and create required
def ulogin(request):
    if request.method != 'POST':
        raise Http404('Only POSTs are allowed')
    try:
        m = Personal.objects.get(name__icontains=request.POST['username'])
        if m.password == request.POST['password']:
            request.session['username'] = m.id
            return HttpResponseRedirect('/')
    except Personal.DoesNotExist:
        return render_to_response('gharnivas/signin.html', {'error' : True }, context_instance=RequestContext(request))

urls.py是:

urlpatterns = patterns('',
    url(r'^$', 'gharnivas.views.index'),#landing page
    url(r'^signin/$','gharnivas.views.signin'),#The view for sign In page
    url(r'^ulogin/$','gharnivas.views.ulogin'),#The view for creating a change in user login
}

然后在登陆页面即first.html我有这个代码:

<table>
  <tr>
    <td>
    {% if user %}
            <div class="whitecolor"  >
                    &nbsp;&nbsp;<a href="">SignOut</a>&nbsp;&nbsp;&nbsp;</div>
    {% else %}
            <a href="/signin/">SignIn</a>&nbsp;&nbsp;&nbsp;
    {% endif %}
        </td>       
    <td><a href="/register/">Register</a>&nbsp;&nbsp;&nbsp;</td>
    <td><a href="/"> Home </a></td>
  </tr>
<table>

但是在输入的url上,我看不到 Sigin Link ,但我得到了 Signout。 当我将 {% if user %} 更改为 {% if not user %} 时,会看到登录。

请让我知道我哪里出错了

I am creating a user login and changing the link of signin to signout .

The views.py i have is:

#This index is for my landing page i.e.first.html where I have Sigin link which needs to hidden on Successful
def index(request):
    if 'username' not in request.session :
        return render_to_response('gharnivas/ghar.html', context_instance=RequestContext(request))
    else:
    u = request.session['username']
    return render_to_response('gharnivas/ghar.html',{ 'user' : u },context_instance=RequestContext(request))


#This here, is the view for my check the user and create required
def ulogin(request):
    if request.method != 'POST':
        raise Http404('Only POSTs are allowed')
    try:
        m = Personal.objects.get(name__icontains=request.POST['username'])
        if m.password == request.POST['password']:
            request.session['username'] = m.id
            return HttpResponseRedirect('/')
    except Personal.DoesNotExist:
        return render_to_response('gharnivas/signin.html', {'error' : True }, context_instance=RequestContext(request))

The urls.py is :

urlpatterns = patterns('',
    url(r'^

Then in the Landing page i.e. first.html I have this code:

<table>
  <tr>
    <td>
    {% if user %}
            <div class="whitecolor"  >
                      <a href="">SignOut</a>   </div>
    {% else %}
            <a href="/signin/">SignIn</a>   
    {% endif %}
        </td>       
    <td><a href="/register/">Register</a>   </td>
    <td><a href="/"> Home </a></td>
  </tr>
<table>

But on the url entered, I dont get to see Sigin Link , but I get Signout.
When i change {% if user %} to {% if not user %} then Signin is seen.

Please let me know where i am going wrong

, 'gharnivas.views.index'),#landing page url(r'^signin/

Then in the Landing page i.e. first.html I have this code:


But on the url entered, I dont get to see Sigin Link , but I get Signout.
When i change {% if user %} to {% if not user %} then Signin is seen.

Please let me know where i am going wrong

,'gharnivas.views.signin'),#The view for sign In page url(r'^ulogin/

Then in the Landing page i.e. first.html I have this code:


But on the url entered, I dont get to see Sigin Link , but I get Signout.
When i change {% if user %} to {% if not user %} then Signin is seen.

Please let me know where i am going wrong

,'gharnivas.views.ulogin'),#The view for creating a change in user login }

Then in the Landing page i.e. first.html I have this code:

But on the url entered, I dont get to see Sigin Link , but I get Signout.
When i change {% if user %} to {% if not user %} then Signin is seen.

Please let me know where i am going wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-11-12 15:26:53

user 始终为真。您必须调用 is_authenticated () 方法。

{% if user.is_authenticated %}

user is always true. You must call the is_authenticated() method.

{% if user.is_authenticated %}
亣腦蒛氧 2024-11-12 15:26:53

在我清除浏览器历史记录并添加 SESSION_EXPIRE_AT_BROWSER_CLOSE = True 后,错误不再存在
在设置.py中。在syncdb之后重新启动Django服务器。
工作过

The error is no more, after i cleared the browser history and also added SESSION_EXPIRE_AT_BROWSER_CLOSE = True
in settings.py. Restarted the Django server after syncdb.
Worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文