【发布时间】:2016-01-06 17:44:31
【问题描述】:
我在编码时遇到了一些问题。我必须在 Python 的论坛中添加一个问题。在数据库中添加新对象后,代码只打印旧对象,没有新对象,即使在数据库中添加了对象。 仅当我再次运行时才会打印新对象。
@app.route('/question/<topic_id>', methods=['GET', 'POST'])
def newquestion(topic_id):
username = 'elena27'
question_ = request.form['question']
new_question = applicativo.newQuestion(username, question_, topic_id)
db.session.add(new_question)
db.session.commit()
db.session.remove()
return render_template('newquestion.html')
这里是applicivo.py:
def countQuestion():
number=int(session.query(Question).order_by('-id').first().id)
return number+1
def newQuestion(username, question, topic):
id = countQuestion()
date = datetime.now()
question = Question(id, topic, username, question, date)
return question
question.html:
<form method="POST" enctype="multipart/form-data" action={{ url_for('newquestion', topic_id=topic_id) }}>
<p>
{{ wtf.quick_form(form) }}
</p>
</form>
newquestion.html 页面只是一个静态页面。
感谢您的帮助!!
【问题讨论】:
-
您不应该将所有问题传递给
render_template()方法吗? -
你用的是什么 rdbms?
标签: python flask sqlalchemy flask-sqlalchemy flask-wtforms