views:

78

answers:

3

I am facing issue with the global variable, when i run in the django development server it works fine, but in apache it doesn't work

here is the code below:

red= "/project3/test/"


def showAddRecipe(request):
    #global objc
    if "userid" in request.session:
        objc["ErrorMsgURL"]= ""
        try:
            urlList= request.POST
            URL= str(urlList['url'])
            URL= URL.strip('http://')
            URL= "http://" + URL

            recipe= __addRecipeUrl__(URL)

            if (recipe == 'FailToOpenURL') or (recipe == 'Invalid-website-URL'):
                #request.session["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
                objc["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
                print "here global_context =", objc
                arurl= HttpResponseRedirect("/project3/add/import/")
                arurl['ErrorMsgURL']= objc["ErrorMsgURL"]
                #return HttpResponseRedirect("/project3/add/import/")
                #return render_to_response('addRecipeUrl.html', objc, context_instance = RequestContext(request))
                return (arurl)
            else:
                objc["recipe"] = recipe
                return render_to_response('addRecipe.html',
                    objc,
                    context_instance = RequestContext(request))
        except:
            objc["recipe"] = ""
            return render_to_response('addRecipe.html',
                objc,
                context_instance = RequestContext(request))
    else:
        global red
        red= "/project3/add/"
        return HttpResponseRedirect("/project3/login")



def showAddRecipeUrl(request):
    if "userid" in request.session:
        return render_to_response('addRecipeUrl.html',
            objc, 
            context_instance = RequestContext(request))
    else:
        global red
        red= "/project3/add/import/"
        return HttpResponseRedirect("/project3/login")


def showLogin(request):
    obj = {}
    obj["error_message"] = ""
    obj["registered"] = ""

    if request.method == "POST":
        if (red == "/project3/test"):
            next= '/project3/recipes'
        else:
            next= red

        try:
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
        except:
            user = authenticate(request=request)

        if user is not None:
            if user.is_active:
                login(request, user)
                request.session["userid"] = user.id

                # Redirect to a success page.
                return HttpResponseRedirect(next)

this code works fine in django development server, but in apache, the url is getting redirected to '/project3/recipes'

+2  A: 

I would guess that you use Apache's CGI capabilities. That means that with each request the script is started anew. Which means that the global variable is initialized with each call.

Apart from that it isn't really a good idea to use globals to store what is in essence session data (with a session, and thus state, per user). Globals are for all users the same, and sessions are per user, which is what you (should) want.

In your case that session's data should probably be stored in some database, as the python interpreter will end when your script is finished and a single page is rendered.

extraneon
yes what you are saying is correct, but i tried using this using the sessions, i tried using request.POST.next and request.POST['next'], request.GET.net and request.GET['next'], but still things were not working as these were coming in empty.Actually, what i am trying to do is, if any user's session is expired or he is not logged in, and is still trying to access any of the internal url, he should be redirected to that url, after he has logged in.hence i thought of using the global variables.
Suhail
You must store the url in `request.session.red`. `request` is also a short-lived object (one request: browser -> server).
Aaron Digulla
Hi Aaron, can you explain a little more, i tried that, but now i am getting an error message, 'SessionStore' object has no attribute 'red'
Suhail
Hey Aaron, thanks man, its working now. i understood, i stored it in request.session['red'] and accessed that. thanks for the help.
Suhail
A: 

As you have been told before, using global objects is a recipe for disaster in a multi-process environment like a live Apache site. You will have multiple users all accessing each others' variables, and this will never work as you want.

extraneon is correct that you should use sessions for this - that is what they are for. From your comment to his answer it is obvious that you have not read the sessions documentation - you should do so now.

Daniel Roseman
Hey Daniel, thanks for the suggestion, the issue is resolved.
Suhail
A: 

Hey guys thanks for the help, yes i know using global variables is an incorrect way of doing it, but i was not able to get it work, but now its working, here's the code below:

def showAddRecipe(request):
    #global objc
    if "userid" in request.session:
        objc["ErrorMsgURL"]= ""
        try:
            urlList= request.POST
            URL= str(urlList['url'])
            URL= URL.strip('http://')
            URL= "http://" + URL

            recipe= __addRecipeUrl__(URL)

            if (recipe == 'FailToOpenURL') or (recipe == 'Invalid-website-URL'):
                #request.session["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
                objc["ErrorMsgURL"]= "Kindly check URL, Please enter a valid URL"
                print "here global_context =", objc
                arurl= HttpResponseRedirect("/project3/add/import/")
                arurl['ErrorMsgURL']= objc["ErrorMsgURL"]
                #return HttpResponseRedirect("/project3/add/import/")
                #return render_to_response('addRecipeUrl.html', objc, context_instance = RequestContext(request))
                return (arurl)
            else:
                objc["recipe"] = recipe
                return render_to_response('addRecipe.html',
                    objc,
                    context_instance = RequestContext(request))
        except:
            objc["recipe"] = ""
            return render_to_response('addRecipe.html',
                objc,
                context_instance = RequestContext(request))
    else:
        request.session['red']= "/project3/add"
        return HttpResponseRedirect("/project3/login")



def showAddRecipeUrl(request):
    if "userid" in request.session:
        return render_to_response('addRecipeUrl.html',
            objc, 
            context_instance = RequestContext(request))
    else:
        request.session['red']= "/project3/add/import"
        return HttpResponseRedirect("/project3/login")


def showLogin(request):
    obj = {}
    obj["error_message"] = ""
    obj["registered"] = ""

    if request.method == "POST":
        if ('red' not in request.session) or (not request.session["red"]):
            print "in if "
            next= '/project3/recipes/'
        else:
            print "in else"
            next= request.session["red"]

        try:
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)
        except:
            user = authenticate(request=request)

        if user is not None:
            if user.is_active:
                login(request, user)
                request.session["userid"] = user.id

                # Redirect to a success page.
                return HttpResponseRedirect(next)
Suhail