【发布时间】:2011-05-06 13:18:16
【问题描述】:
我正在创建用户登录并将 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 %} 时,就会看到登录。
请告诉我哪里出错了
【问题讨论】:
标签: django authentication django-templates django-views