Flask 同时为不同的网站访问者分配不同的 ID,并将他们的输入信息写入 json 文件
Flask Assign different ID to different website visitor at the same time and write their input information into json file
我正在使用 FLASK 创建一系列网站来做一个调查,我想 运行 服务器一次并邀请多个参与者帮我填写网页上的表格。
1。
我的目标是为不同的访问者分配一个唯一的 ID(目前使用 python UUID)并将他们的输入信息保存到不同的 JSON 文件中。 (例如,A的id是00001,提交表单后,信息将保存在名为00001.json的文件中,B的信息将保存在00002.json中)。目前,我只是在服务器启动时创建一个UUID 运行ning,每个访问者的信息都会被重写在同一个文件中,(我想要的是n个访问者会产生n个文件)。
2。
我想向不同的访问者展示不同的调查问题。
我很困惑是否与Multi-thread有关? Session?曲奇饼? Flask-login.
希望有人能指导我。非常感谢!
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
#it ideally should randomly select different info/question no. to different user
Idx = random.sample(range(0,10), 5)
@app.route("/")
def index():
return render_template("surveyPage.html", data=Idx)
# write input information from webpage to JSON files, each visitor ideally should have their own JSON file.
@app.route("/record")
def recordData():
if request.method == 'POST':
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()
if __name__ == "__main__":
app.config['JSON_SORT_KEYS'] = False
app.run(debug = True)
# app.config["TEMPLATES_AUTO_RELOAD"] = True
您正在请求处理程序之外定义 assignmentID
、jsonFileName
、jsonSurvey
和 Idx
,这意味着它们将被设置一次:当服务器启动时。 jsonSurvey
因此每个请求的值都相同,这意味着您存储的每个新调查都将覆盖前一个。
解决方案是将定义移动到请求处理程序中,以便您在每个请求上生成新值。
@app.route("/")
def index():
Idx = random.sample(range(0,10), 5)
return render_template("surveyPage.html", data=Idx)
@app.route("/record")
def recordData():
if request.method == 'POST':
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()
我正在使用 FLASK 创建一系列网站来做一个调查,我想 运行 服务器一次并邀请多个参与者帮我填写网页上的表格。
1。 我的目标是为不同的访问者分配一个唯一的 ID(目前使用 python UUID)并将他们的输入信息保存到不同的 JSON 文件中。 (例如,A的id是00001,提交表单后,信息将保存在名为00001.json的文件中,B的信息将保存在00002.json中)。目前,我只是在服务器启动时创建一个UUID 运行ning,每个访问者的信息都会被重写在同一个文件中,(我想要的是n个访问者会产生n个文件)。
2。 我想向不同的访问者展示不同的调查问题。
我很困惑是否与Multi-thread有关? Session?曲奇饼? Flask-login.
希望有人能指导我。非常感谢!
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
#it ideally should randomly select different info/question no. to different user
Idx = random.sample(range(0,10), 5)
@app.route("/")
def index():
return render_template("surveyPage.html", data=Idx)
# write input information from webpage to JSON files, each visitor ideally should have their own JSON file.
@app.route("/record")
def recordData():
if request.method == 'POST':
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()
if __name__ == "__main__":
app.config['JSON_SORT_KEYS'] = False
app.run(debug = True)
# app.config["TEMPLATES_AUTO_RELOAD"] = True
您正在请求处理程序之外定义 assignmentID
、jsonFileName
、jsonSurvey
和 Idx
,这意味着它们将被设置一次:当服务器启动时。 jsonSurvey
因此每个请求的值都相同,这意味着您存储的每个新调查都将覆盖前一个。
解决方案是将定义移动到请求处理程序中,以便您在每个请求上生成新值。
@app.route("/")
def index():
Idx = random.sample(range(0,10), 5)
return render_template("surveyPage.html", data=Idx)
@app.route("/record")
def recordData():
if request.method == 'POST':
assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"
print("READING FORM")
with open(jsonSurvey, 'w') as f:
json.dump(request.form, f)
f.close()