当我尝试登录时,它显示:
KeyError at /admin/
'loggedin'
只有当我使用正确的用户名和密码提交时,它才会显示此错误。所以我认为这是关于我的管理员视图..
这是我的登录视图:
def log_user_in(request):
if request.method=='POST':
uname = request.POST['username']
password = request.POST['password']
user = authenticate(username=uname, password=password)
form = LoginForm(request.POST)
if form.is_valid():
if user is not None:
request.session['loggedin']="djangoo"
login(request, user)
return HttpResponseRedirect('/admin/')
else:
form = LoginForm()
return render_to_response('login.html',{'form':form,},context_instance=RequestContext(request))
并注销视图:
def log_user_out(request):
user = request.user
try:
del request.session['loggedin']
except KeyError:
pass
logout(request)
return HttpResponseRedirect('/blog/%s/'%(user))
和管理员视图:
def admin_view(request):
if request.session['loggedin'] == "djangoo":
#other codes..
这是我的 urls.py:
urlpatterns = patterns('blog.views',
url(r'^superadmin/', include(admin.site.urls)),
url(r'^blog/(?P<username>[-\w]+)/$',view='index', name='index'),
url(r'^blog/(?P<username>[-\w]+)/post/(?P<postslug>[-\w]+)',view='single_post', name='view_blog_post'),
url(r'^login/$', view='log_user_in'), # i log in to admin page from this url
url(r'^admin/$', view='admin_view', name='admin'),# to this url
url(r'^admin/logout/$', view='log_user_out', name='logout'),
url(r'^admin/post/add/$', view='add_post',name='addpost'),
url(r'^admin/post/edit/(?P<post_id>\d+)', view='edit_post', name='editpost'),
url(r'^admin/post/delete/(?P<post_id>\d+)', view='delete_post', name='deletePost'),
)