我被要求创建一个简单的Flask应用程序,它只存储一个文本 字段,在单个网页上,用户应在其上看到带有“消息”的表单 领域。表单下方应该是一个现有消息列表 数据库。当用户在“消息”字段中键入内容并提交 表单中,“message”应该保存到SQLite中的表中。 AND保存邮件后,用户应返回包含表单和邮件列表的页面。
我陷入了困境 我无法提交消息 我的页面确实从我创建数据库时已经存储在数据库中的数据库中检索消息。
但有人可以指导代码提交表格中填写的信息。 我的message.html代码是:
{% extends "template.html "%}
{% block content %}
<h2>You are in the Message Page</h2>
<br/>
<p><h4>In this page, You can view the Existing Messages and can also Submit your own message.</h4></p>
<br/><br/>
<h3>Enter Your Message:</h3><br/>
<form action="" method='POST'>
<dl>
<dt>Message:
<dd><input type="text" name=msg_msg maxlength=80 style="width:300px">*Maximum Length = 80
</dl>
<input type=submit value="Submit Message">
</form>
<h3>The Existing Messages:</h3>
{% for item in message %}
Msg_ID: <B>"{{ item.msg_id }}"</B><br/>Message: {{ item.msg_msg }} <br/><br/>
{% endfor %}
{% endblock %}
and my routes.py code for this is:
@app.route('/message')
def message():
g.db = connect_db()
cur = g.db.execute('select msg_id, msg_msg from msg')
message = [dict(msg_id=row[0], msg_msg=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template('message.html', message=message)
@app.route('/message', methods=['GET', 'POST'])
def message_1():
error = None
if request.method == 'POST':
for request.form['input_msg'] = text:
g.db = connect_db()
cur = g.db.execute('insert into msg_msg from msg')
message = [dict(msg_id=row[0], msg_msg=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template('message.html', message=message)
#error = 'Invalid Entry, Please Try Again.'
else:
# session['logged_in'] = True
return redirect(url_for('message'))
return render_template('message.html' , error=error)
答案 0 :(得分:1)
使用1条消息视图
@app.route('/message', methods=['GET', 'POST'])
def message():
cur = db.execute('select id,msg from msg_msg')
message = [dict(msg_id=row[0], msg_msg=row[1]) for row in cur.fetchall()]
if request.method == 'POST':
g.db = connect_db()
cur = g.db.execute('insert into msg_msg (msg) values (?)',request.form['input_msg'])
message = [dict(msg_id=row[0], msg_msg=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template('message.html' , message = message)
<强> message.html 强>
{% extends "template.html "%}
{% block content %}
<h2>You are in the Message Page</h2>
<br/>
<p><h4>In this page, You can view the Existing Messages and can also Submit your own message.</h4></p>
<br/><br/>
<h3>Enter Your Message:</h3><br/>
<form action={{ url_for('message') }} method='POST'>
<dl>
<dt>Message:
<dd><input type="text" name=msg_msg maxlength=80 style="width:300px">
</dl>
<input type=submit value="Submit Message">
</form>
<h3>The Existing Messages:</h3>
{% for item in message %}
Msg_ID: <B>"{{ item.msg_id }}"</B><br/>Message: {{ item.msg_msg }} <br/><br/>
{% endfor %}
{% endblock %}