我使用django fobi允许用户创建自己的表单。我想根据这些表格制作一些报告和统计数据。
我有一个有效的python代码,但我不知道如何翻译" django工作模板中的代码。
数据如下:
saved_data = '''
[
[{
"label": "Question 1?",
"html_name": "question_1",
"first_name": "John",
"last_name": "Doe",
"email": "john@doe.com",
"answer": "Yes"
}, {
"label": "Question 2?",
"html_name": "question_2",
"first_name": "John",
"last_name": "Doe",
"email": "john@doe.com",
"answer": ["Red", "White"]
}],
[{
"label": "Question 1?",
"html_name": "question_1",
"first_name": "Jane",
"last_name": "Mary",
"email": "mary@jane.com",
"answer": "No"
}, {
"label": "Question 2?",
"html_name": "question_2",
"first_name": "Jane",
"last_name": "Mary",
"email": "mary@jane.com",
"answer": ["Red", "Black"]
}]
]
'''
data = json.loads(saved_data)
answer = {}
for x in data:
for y in x:
if type(y['answer']) is list:
for an in y['answer']:
answer.setdefault(y['label'], {})\
.setdefault('answer', []).append(an)
answer.setdefault(y['label'], {}).setdefault('full_name', []).append(y['first_name'] + ' ' + y['last_name'])
answer.setdefault(y['label'], {}).setdefault('email', []).append(y['email'])
elif type(y['answer']) is str:
answer.setdefault(y['label'], {})\
.setdefault('answer', []).append(y['answer'])
answer.setdefault(y['label'], {}).setdefault('full_name', []).append(y['first_name'] + ' ' + y['last_name'])
answer.setdefault(y['label'], {}).setdefault('email', []).append(y['email'])
for questions, answers in answer.items():
print('\n', questions)
counting = Counter(answers['answer'])
for key, value in counting.items():
print(key, value)
for answer in zip(answers['full_name'], answers['answer']):
if key == answer[1]:
print(answer[0])
如何让它在django 1.11模板中运行?