2010-06-14 139 views
0

我現在面臨的問題與全局變量,當我在它工作正常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是被重定向到/項目3 /食譜「

+0

如果您在函數中查看全局變量'red'的值是在用戶會話過期或未登錄之後設置的,但正在嘗試訪問內部URL,那麼我將得到全局變量red的正確值,即當用戶在django開發服務器上運行時,用戶試圖訪問的url而無需登錄,但相同的代碼在apache服務器上無法運行。 – Suhail 2010-06-14 08:12:02

回答

3

我猜你使用Apache的CGI能力。這意味着每次請求都會重新啓動腳本。這意味着每次調用都會初始化全局變量。

除此之外,它是不是真的要使用全局變量存儲什麼本質上是會話數據(與會話,因此狀態,每個用戶)是個好主意。全局用戶對所有用戶都是一樣的,會話是每個用戶的,這是你(應該)想要的。

在你的情況是會話的數據可能應該存放在一些數據庫,Python解釋器將結束時,你的腳本完成和單頁呈現。

+0

是你說的是正確的,但我試着使用這個會話,我嘗試使用request.POST.next和request.POST ['next'],request.GET.net和request.GET ['next'] ,但仍然沒有工作,因爲這些都是空的。 其實,我試圖做的是,如果任何用戶的會話過期或他沒有登錄,並仍試圖訪問任何內部URL的,他應該被重定向到URL,他在登錄後, 因此我想到了使用全局變量。 – Suhail 2010-06-14 08:37:58

+1

您必須將url存儲在'request.session.red'中。 'request'也是一個短暫的對象(一個請求:瀏覽器 - >服務器)。 – 2010-06-14 08:59:02

+0

嗨阿龍,你可以解釋一下,我試過了,但現在我得到一個錯誤信息,「SessionStore」對象有沒有屬性「紅」 – Suhail 2010-06-14 09:36:32

0

正如你之前已經告知,使用全局對象是在多進程環境就像一個活的Apache站點是一個災難。您將有多個用戶訪問彼此的變量,並且這絕對不會按照您的要求工作。

extraneon是正確的,你應該使用這個會話 - 這是他們所爲。從你的評論到他的回答顯然你沒有閱讀sessions documentation - 你現在應該這樣做。

+0

嘿丹尼爾,謝謝你的建議,問題解決了。 – Suhail 2010-06-14 10:09:05

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)