0
這是一個相當愚蠢的問題,但我逐步瞭解了該功能,但無法弄清楚導致問題的原因。渲染對象以在Flask中查看時,屬性丟失
我正在動態地添加一個屬性給我通過SQLAlchemy從數據庫中獲取的對象,並且在調用返回render_template之前附加了對象,除了found_survey.questions列表中變量的最後一個附加屬性丟失了。即found_survey.questions [0] .choices不存在(但它在一點上),但found_survey.questions [-1] .choices。
我似乎無法弄清楚發生了什麼事情......我認爲這可能是因爲數據庫對象的原因(因爲我沒有提交調查,但我不打算,我只是追加了值,因此它以正確的邏輯傳遞給視圖)。
非常感謝您的幫助;我真的卡住了...
@app.route('/survey/<survey_id>', methods=['GET', 'POST'])
@login_required
def survey(survey_id):
form = UserSubmitForm()
found_survey = Survey.query.filter_by(id=survey_id).first()
if request.method == 'POST' and form.validate_on_submit():
print("input", form.answer_raw.data, form.category_list.data)
user_answer = Answer(note=found_survey.name,
answer_raw=form.answer_raw.data,
timestamp=datetime.utcnow(),
ip=request.remote_addr)
user_answer.user_id = g.user.id
user_answer.survey_id = survey_id
# other processing omitted
db.session.add(user_answer)
elif request.method != "POST":
for q in found_survey.questions:
q.choices = []
text_list = q.choice_text_string.split(',')
value_list = q.choice_value_string.split(',')
for i, text in enumerate(text_list):
q.choices.append((text, value_list[i]))
return render_template('survey.html',
form=form,
survey=found_survey,
is_editing=False)
的東西是視圖呈現基於調查問題的HTML的另一塊,所以我想要將信息和相同的對象粘在一起,而不必傳遞一個單獨的對象(Jinja2的模板語言的功能是有限的),所以我想直接修改found_survey對象 – yifanwu