我在一个用于计算用户详细信息的应用程序中工作。但不知何故,一个用户的价值观会改变另一个用户的价值观。
下面是代码片段
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 的结果。
阅读有关使用实例变量的内容,但仍然不知道如何使其工作
解决方案很简单:停止在 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必须是显示结果的模板
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)