Web应用程序共享相同的内存存储

时间:2014-01-06 16:43:58

标签: python google-app-engine python-2.7 web

我在一个用于计算用户详细信息的应用程序中工作。但不知何故,用户的价值改变了另一个用户的价值。 下面是代码片段

def Compute_UserScore(self, details, ques_no):
    try:
        if(HomePage.answer_.strip() == ""):
            self.response.write("""<script type = "text/javascript">
            alert("Dear User, You can not answer same answer twice.. Take test Again !");
            </script>""")
            self.redirect('/otherPages/subjectSelect.html')
        else:
            count = 0
            HomePage.ans_no = 0
            HomePage.unans_no = 0
            HomePage.correct_no = 0
            HomePage.wrong_no = 0
            HomePage.failed_ques = list()
            HomePage.answer_ = HomePage.answer_.strip()
            question_1 = HomePage.question_.split(" gcdc_split_format ")
            while (count != (ques_no)):
                user_answer = str(details[count]).strip().capitalize()
                real_answer = str(HomePage.answer_[count]).strip().capitalize()
                if (len(str(user_answer).strip()) == 1):
                    HomePage.ans_no = HomePage.ans_no + 1
                    if(user_answer.strip() == real_answer.strip()):
                        HomePage.correct_no = HomePage.correct_no + 1
                    else:
                        HomePage.wrong_no = HomePage.wrong_no + 1
                        HomePage.failed_ques.append(str("No. " + str(int((count + 1))) + "  " + str(question_1[count])))
                else:
                    HomePage.unans_no = HomePage.unans_no + 1
                count = count + 1
            HomePage.answer_ = ""
    except:
        self.redirect('/')
    return " "

这就是我的主页的样子

class HomePage(webapp2.RequestHandler):
    percentage = None
    subject_answered = None
    username_ = None
    email_ = None
    super_date = None
    answer_ = " "
    question_ = " "
    failed_ques = list()
    wrong_no = 0
    correct_no = 0
    ans_no = 0
    unans_no = 0

问题是,当用户A进行测试时,他会看到另一个用户B的结果。 阅读有关使用实例变量,但仍然没有计算如何使其工作

1 个答案:

答案 0 :(得分:3)

解决方案很简单:停止在Web开发中设置类变量! :) Web请求是无状态的,这意味着您永远不知道请求之间发生了什么 - 设置类变量和重定向之间。

使用数据库存储具有用户登录名/名称的临时数据(或使用散列/随机来保证安全性)或通过参数(隐藏或“?”之后)将值发送到其他html页面。 使用数据库更好,如果你不想这样,那么通过http发送值(隐藏在html中)。这是一个解决方案版本(没有数据库):

1.使用普通的html表单并为此表单编写处理程序 - 问题页面。

2.在处理程序中写入get方法如下:

def post(self, some_parameters):
   ...
   self.render('homepage.html', {'ans_no': ans_no,\
                                'uans_no': uans_no ...}) 

3.homepage.html必须是显示结果的模板