web app is sharing the same memory storage [duplic

2020-04-30 16:16发布

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

1条回答
Rolldiameter
2楼-- · 2020-04-30 16:50

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

查看更多
登录 后发表回答