我正在创建用户登录信息并将登录的链接更改为登出。
我拥有的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" >
<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>
但是在输入的网址上,我没有看到Sigin Link,但我得到了Signout。
当我将{% if user %}
更改为{% if not user %}
时,会看到Signin。
请让我知道我哪里出错了
答案 0 :(得分:0)
user
永远是真的。您必须调用is_authenticated()
方法。
{% if user.is_authenticated %}
答案 1 :(得分:0)
在我清除浏览器历史记录并添加SESSION_EXPIRE_AT_BROWSER_CLOSE = True
后,错误已不复存在
在settings.py中。在syncdb之后重新启动Django服务器。
工作