python全局变量不在apache中工作

时间:2010-06-14 08:08:36

标签: python django apache global-variables

我面临全局变量的问题,当我在django开发服务器中运行时,它工作正常,但在apache中它不起作用

这是下面的代码:

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)

这个代码在django开发服务器上工作正常,但在apache中,url被重定向到'/ project3 / recipes'

3 个答案:

答案 0 :(得分:3)

我猜你会使用Apache的CGI功能。这意味着每次请求都会重新启动脚本。这意味着每次调用都会初始化全局变量。

除此之外,使用全局变量存储本质上会话数据(使用会话,因此状态,每个用户)并不是一个好主意。 Globals适用于所有用户,会话是每个用户,这是您(应该)想要的。

在您的情况下,会话的数据可能应该存储在某个数据库中,因为python解释器将在脚本完成并呈现单个页面时结束。

答案 1 :(得分:0)

正如您之前所说的那样,使用全局对象是一个在多进程环境(如实时Apache站点)中发生灾难的方法。您将有多个用户访问彼此的变量,这将永远不会按您的意愿工作。

extraneon是正确的,您应该使用会话 - 这就是它们的用途。从您的评论到他的答案很明显,您还没有阅读sessions documentation - 您现在应该这样做。

答案 2 :(得分:0)

嘿伙计们感谢您的帮助,是的,我知道使用全局变量是一种不正确的方式,但我无法让它工作,但现在它的工作,这里是下面的代码:

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)