python global variable not working in apache

本小妞迷上赌 提交于 2019-12-25 04:41:10

问题


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'


回答1:


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.




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/3035776/python-global-variable-not-working-in-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!