Django代码在本地和服务器上的行为不同 [英] Django code behaving differently on local and server
问题描述
以下代码在本地运行正常,但是当我将其部署到服务器时,我会收到以下错误。我相信问题是当运行代码时,位置 images [1]
没有任何值。但是我不明白为什么这只是在服务器上发生,而不是在本地机器上发生。
- 本地: OS X 10.9.5,Python 2.7.5,Django 1.6.2
- 服务器: Ubuntu 12.04,Apache 2.2.22,mod_wsgi版本:3.3-4ubuntu0.1,Python 2.7.3,Django 1.6
我已经标记了 ### line 143 ###
这与以前的问题有关有关于在Django SessionWizardView中将数据从一个页面持续到下一个的地方,建议我使用一个会话对象存储我想要保留的数据以供后期页面使用。
images []
PATH_ONE_IMAGES = ['P1D1.jpg','P2D2.jpg','P3D3.jpg']
class SurveyWizardOne(SessionWizardView):
def get_context_data(self,form,** kwargs)
context = super(SurveyWizardOne,self).get_context_data(form,** kwargs)
step = int(self.steps.current)
如果步入范围(5,19):
如果步入(5,6,7):
image = random.choice(PATH_ONE_IMAGES)
图像.insert(step - 5,image)
PATH_ONE_IMAGES.remove(image)
上下文['display_image'] = image
如果step == 5:
self.request.session ['first_image'] = images [0]
self.request.session.get('first_image','default_request_session_value')
elif step == 6:
self.request.session ['second_image'] = images [1] ### line 143 ###
self.request.session.get('second_image','default_request_session_value')
elif step == 7:
self.request.session ['third_image'] = images [2]
self.request.session.get('third_image','default_request_session_value')
elif step == 8:
slider_value = self.request.POST.get('slider_value')
如果slider_value不为None:
slider_DV_values.insert(step - 5,slider_value )
上下文['first_image'] = self.request.session ['first_image']
上下文['second_image'] = self.request.session ['second_image']
context ['third_image'] = self.request.session ['third_image']
上下文['first_slider'] = slider_DV_values [0]
上下文['second_slider'] = slider_DV_values [1]
context ['third_slider'] = slider_DV_values [2]
....
....
返回上下文
如果有人可以解释为什么这个错误在部署中而不是在本地发生,并提供修复,将不胜感激。
谢谢,Deepend
我不是很善于解释事情,但我会试一试。
关键问题是因为您有全局定义的图像
列表。由于您在单个进程,因此它适用于您本地-or-address-portrel =nofollow noreferrer>本地django开发服务器。每个请求共享相同的内存空间,在同一进程中访问相同的图像
变量。
您将应用程序部署到服务器,您正在输入多进程环境 - 简单来说,图像
的值不会跨多个共享进程,因此不会在不同请求之间共享。换句话说,您在不同请求中插入不同的图像
列表:
不要使用全局变量,真的,请不要。
要解决问题,您需要保留图像
其他地方 - 在会话内或数据库中:
def get_context_data(self, form,** kwargs)
context = super(SurveyWizardOne,self).get_context_data(form,** kwargs)
step = int(self.steps.current)
image = self.request.session.get('images',[])
...
self.request.session ['images'] =图像
返回上下文
This is an old question I never got a satisfactory answer for and I have decided to add a bounty here so that someone might take an interest and figure out what is going on.
The below code works fine locally but when I deploy it to the server I keep getting the below error. I believe the issue is that there is no value at the position images[1]
when the code is run. However I don't understand why this only happens on the server and not on my local machine.
- Local: OS X 10.9.5, Python 2.7.5, Django 1.6.2
- Server: Ubuntu 12.04, Apache 2.2.22, mod_wsgi Version: 3.3-4ubuntu0.1, Python 2.7.3, Django 1.6
I have marked ### line 143 ###
below.
This is related to a previous issue I was having in relation to persisting data from one page to the next in a Django SessionWizardView where it was recommended that I use a session object to store the data I want to persist for use in later pages.
images []
PATH_ONE_IMAGES = ['P1D1.jpg', 'P2D2.jpg', 'P3D3.jpg']
class SurveyWizardOne(SessionWizardView):
def get_context_data(self, form, **kwargs):
context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)
step = int(self.steps.current)
if step in range (5, 19):
if step in (5, 6, 7):
image = random.choice(PATH_ONE_IMAGES)
images.insert(step - 5, image)
PATH_ONE_IMAGES.remove(image)
context['display_image'] = image
if step == 5:
self.request.session['first_image'] = images[0]
self.request.session.get('first_image', 'default_request_session_value')
elif step == 6:
self.request.session['second_image'] = images[1] ### line 143 ###
self.request.session.get('second_image', 'default_request_session_value')
elif step == 7:
self.request.session['third_image'] = images[2]
self.request.session.get('third_image', 'default_request_session_value')
elif step == 8:
slider_value = self.request.POST.get('slider_value')
if slider_value is not None:
slider_DV_values.insert(step - 5, slider_value)
context['first_image'] = self.request.session['first_image']
context['second_image'] = self.request.session['second_image']
context['third_image'] = self.request.session['third_image']
context['first_slider'] = slider_DV_values[0]
context['second_slider'] = slider_DV_values[1]
context['third_slider'] = slider_DV_values[2]
....
....
return context
If anyone can explain why this error is happening in deployment and not locally and provide a fix it would be greatly appreciated.
Thanks, Deepend
I'm not good at explaining things, but I'll give it a try.
The key problem is because you have images
list defined globally. It works for you locally since you have a single process running under the local django development server. Every request is sharing the same "memory space", accessing the same images
variable in the same process.
When you deploy your app to the server, you are entering a multi-process environment - in simple words, the value of images
is not shared across multiple processes and, therefore, it is not shared between different requests. In other words, you are inserting into different images
lists in different requests:
Do not use global variables, really, please don't.
To fix the issue you need to keep the images
somewhere else - inside a session, or in the database:
def get_context_data(self, form, **kwargs):
context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)
step = int(self.steps.current)
images = self.request.session.get('images', [])
...
self.request.session['images'] = images
return context
这篇关于Django代码在本地和服务器上的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!