Hello

I have simple application which checks for username and password in 
database as well as looks in session for key "user_id". If user exists and 
login gets successfull then page is redirected to admin page. Here problem 
is I am storing username in session in one view and retrieving it another 
but at the time of retrieval it says that the key does not exist in 
session. 
I have just started learning Django and this is my first project. Please 
help me.

Here is my views.py : 

====================================================================================================
# Create your views here.
from BaseHTTPServer import BaseHTTPRequestHandler
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from webapp.models import User


def index(request):

    if request.session.get("user_id", False):
        print "Used logged in"
        return HttpResponseRedirect("webadmin")
    else:
        print "Used not logged in"
        return  render_to_response("index.html", {})

@csrf_exempt
def login(request):

    username = request.POST.get("username", None)
    password = request.POST.get("password", None)
    vals = {}
    if not username or not password:
        print "Please enter credentials"
        vals['error'] = "Invalid credentials"
        return HttpResponseRedirect("/")
    elif not isUserExists(username, password):
        print "Invalid credentials"
        vals['error'] = "Invalid credentials"
        return HttpResponseRedirect("/")
    else:
        request.session['user_id'] = username
        request.session.save()
        request.session.modified = True
        print "&&&&&&&&&&&&&&&&& : " + request.session['user_id']
        print "Sessino value written"
        return HttpResponseRedirect("webadmin")

def webadmin(request):
    '''
        Controller to handle /webadmin url
    '''
    return render_to_response("admin.html", {})

def isUserExists(uname, pwd):
    '''
        Check if user exists in database or not
    '''
    users = User.objects.filter(username=uname, password=pwd)
    if len(users) >0:
        print "Access granted"
        return True
    else:
        print "Access denied"
        return False
#########################################################################################

I am using file based session and has following setting in settings.py file.

SESSION_ENGINE = "django.contrib.sessions.backends.file"
SESSION_SAVE_EVERY_REQUEST=True


Thanks

Malhar Vora

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/650e3fc0-6070-4d77-90b1-ec6c47b9abaf%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to