简体   繁体   English

Django不记得上下文

[英]Django doesn't remember context

I'm new to Django and i'm trying to set up an existing project on new server. 我是Django的新手,正在尝试在新服务器上设置现有项目。

Django starts, but it behaves very strange. Django启动了,但是它的行为很奇怪。 I have the following code: 我有以下代码:

someVar = None

def first(request):
    global someVar
    someVar = 'modified'

def second(request):
    return HttpResponse(someVar) # prints 'None'

I mapped this methods to URLs. 我将此方法映射到URL。 When I call 'first' method and then 'second', the expected output is 'modified', but actually it is 'None' 当我先调用“ first”方法,然后调用“ second”方法时,预期的输出是“ modified”,但实际上是“ None”

It seems like Apache starts the application on each request, like if it was some cgi script. 似乎Apache在每个请求上启动应用程序,就像它是某些cgi脚本一样。 Any ideas why this is happening? 任何想法为什么会这样?

I'm using Apache2.2 with mod_wsgi and Django 1.5.9. 我正在将Apache2.2与mod_wsgi和Django 1.5.9一起使用。 Django project is outside of Apache's document root. Django项目在Apache文档根目录之外。 Here is Apache host configuration file: 这是Apache主机配置文件:

WSGIScriptAlias / /path/mysite/mysite/wsgi.py
WSGIPythonPath /path/mysite

<Directory /path/mysite/mysite>
     <Files wsgi.py>
         Order deny,allow
        Allow from all
    </Files>
</Directory>

Module-level variables are only shared between requests that use the same process. 模块级变量仅在使用相同进程的请求之间共享。 Apache is almost certainly spinning up multiple processes to serve your site. 几乎可以肯定,Apache会分拆多个进程来为您的站点提供服务。 If your subsequent requests happen to go to the same process that served the initial one, you will see the change; 如果随后的请求恰好与最初的请求相同,则将看到更改; otherwise you won't. 否则你不会的。

If you really need to share data between requests no matter which process serves them, you should use a more persistent place, such as the session or the database. 如果确实需要在请求之间共享数据,而不管是由哪个进程处理的,则应该使用更持久的位置,例如会话或数据库。

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

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