Python中文
首页
教程
问答
标签
搜索
登录
注册
python全局变量在apach中不工作
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正面临全局变量的问题,当我在django开发服务器上运行时,它可以正常工作,但在apache中却不能工作</p> <p>代码如下:</p> <pre><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></pre> <p>此代码在django开发服务器中运行良好,但在apache中,url被重定向到“/project3/recipes”</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我想您应该使用Apache的CGI功能。这意味着,对于每个请求,脚本都会重新启动。这意味着全局变量在每次调用时初始化。在</p> <p>除此之外,使用全局变量来存储本质上是会话数据(每个用户有一个会话,因此是状态)并不是一个好主意。全局参数对于所有用户都是相同的,会话是针对每个用户的,这是您(应该)想要的。在</p> <p>在您的情况下,会话的数据可能应该存储在某个数据库中,因为python解释器将在脚本完成并呈现单个页面时结束。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
5 回答
如何在乒乓球游戏中阻止球
4 回答
如何在乘法和模中不乘空间?
1 回答
如何在乘法和除以2个不同的数字之间进行交换?
9 回答
如何在也是数据一部分的单个字符上拆分大字符串
1 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
2 回答
如何在事件wxWidgets中传递自定义数据
10 回答
如何在事件中使用lambda i=i?
3 回答
如何在事件中心只接收最近的数据
9 回答
如何在事件发生之前保持云函数运行?
10 回答
如何在事件发生后使页面重定向到同一页面
8 回答
如何在事件回调之间保持python生成器的状态
2 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
4 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
7 回答
如何在事件处理程序中访问外部对象?
3 回答
如何在事件循环中将协程打包为正常函数?
6 回答
如何在事件循环之外运行协同程序?
10 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
8 回答
如何在事件文件中只保留一份摘要?
5 回答
如何在事件模板中添加事件
1 回答