网络应用程序共享相同的内存存储空间 [英] web app is sharing the same memory storage

查看:94
本文介绍了网络应用程序共享相同的内存存储空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用于计算用户详细信息的应用程序中工作。但不知何故,用户的价值改变了另一个用户的价值。
下面是代码片段:
$ b $ pre code def def Compute_UserScore(self,details,ques_no):
try :
if(HomePage.answer_.strip()==):
self.response.write(< script type =text / javascript>
alert (亲爱的用户,你不能回答相同的答案两次..再次测试!);
< / 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 = s如果(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_ =
除外:
self.redirect('/')
返回

这就是我的主页的样子

  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的结果。
阅读使用实例变量,但仍然没有想到如何使它工作

解决方案

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

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

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



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

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

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


I working in a app that i use to compute user details. But somehow, the values of a user alter that of another user. Below is a fragment of the code

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 " "

and this is how my homepage looks like

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

The problem is, when a user A, take a test, He sees the result of another user B. Read about Using instance variable, but still have not figure ouut how to make it work

解决方案

Solution is simple: Stop setting class variables in web development! :) Web requests are stateless, it's mean you never know what's happen between requests - between setting class variable and redirect.

Use database to store temporary data with user login/name (or use hashing/random for security) or send values by parameters (hidden or after '?') to other html page. Using database is better, if you don't want this then send values (hidden in html) over http. Here is one version of solution (without database):

1.Use normal html form and write handler for this form - question page.

2.In handler write get method like this:

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

3.homepage.html have to be template for showing results

这篇关于网络应用程序共享相同的内存存储空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆