0

when i try to login it shows :

KeyError at /admin/

'loggedin'

it shows this error only when i submit with correct username and pwd. so i think it is about my admin view..

here is my log in view :

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))

and log out view :

def log_user_out(request):
    user = request.user
    try:
        del request.session['loggedin']
    except KeyError:
        pass
    logout(request)
    return HttpResponseRedirect('/blog/%s/'%(user))

and admin view :

def admin_view(request):

    if request.session['loggedin'] == "djangoo":
    #other codes..

here is my 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'),

)
5
  • 1
    use if request.session.get('loggedin') == 'djangoo': Sep 14, 2012 at 12:54
  • Have you activated the SessionMiddleware?
    – Babu
    Sep 14, 2012 at 12:55
  • yes i activated. and one more thing. this error is not always shown.i get this error sometimes. now i changed it to request.session.get() .
    – alioguzhan
    Sep 14, 2012 at 12:57
  • get() will try to fetch the key value and if the key is not there, it will just return an empty string. post your urls.py and mention when you're getting this error.
    – Babu
    Sep 14, 2012 at 12:58
  • @drTerminal much better to post error tracebak, but obviously error is coming from if request.session['loggedin'] == "djangoo": Sep 14, 2012 at 13:08

1 Answer 1

4

When you logged out, you manually delete request.session['loggedin'] key, and when you browse to admin page, request.session['loggedin'] fails because you already delete that key.

You can control if request.session has loggedin key with:

`loggedin` in request.session

So, writing your admin view like:

def admin_view(request):

    if `loggedin` in request.session and request.session['loggedin'] == "djangoo":
    #other codes..

Or simply use

if request.session.get('loggedin') == 'djangoo'

as @Alexander Larikov said

EDIT: I need to mention that, it is good to check if a key exists in dictionary or dictionary-like structure, especially if you also may delete that key in somewhere else.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.