蟒蛇全局变量在Apache中不工作 [英] python global variable not working in apache

查看:130
本文介绍了蟒蛇全局变量在Apache中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的问题与全局变量,当我在它工作正常Django开发服务器上运行,但在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

这里是低于code:

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)

这code工作在Django开发服务器罚款,但在Apache中,网址是越来越重定向到/项目3 /食谱

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

推荐答案

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

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.

在您的情况下会话的数据或许应该被存储在某些数据库,蟒蛇间preTER将结束时,你的脚本完成,并在一个页面的呈现方式。

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.

这篇关于蟒蛇全局变量在Apache中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆