简体   繁体   English

Python全局变量在Apache中不起作用

[英]python global variable not working in apache

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 我在全局变量中遇到问题,当我在Django开发服务器中运行时,它工作正常,但是在apache中,它不工作

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' 此代码在Django开发服务器中工作正常,但在apache中,URL被重定向到'/ project3 / recipes'

I would guess that you use Apache's CGI capabilities. 我猜您会使用Apache的CGI功能。 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. 在您的情况下,会话的数据可能应该存储在某个数据库中,因为当脚本完成并呈现单个页面时,python解释器将终止。

As you have been told before, using global objects is a recipe for disaster in a multi-process environment like a live Apache site. 如前所述,在像实时Apache网站这样的多进程环境中,使用全局对象是灾难的根源。 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. extraneon是正确的,您应该为此使用会话-这就是它们的用途。 From your comment to his answer it is obvious that you have not read the sessions documentation - you should do so now. 从您的评论到他的回答,很明显,您尚未阅读会议文档 -您现在应该阅读。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM