Django 模板检查用户是否已通过身份验证并且存在
我正在创建用户登录并将 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" >
<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>
但是在输入的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
user
始终为真。您必须调用is_authenticated ()
方法。user
is always true. You must call theis_authenticated()
method.在我清除浏览器历史记录并添加
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