我需要向渲染模板行中的html页面发送一些数据。我不认为该行甚至可以在该方法的POST版本中执行,因为我可以更改html页面并且它根本不会更改,但是如果我在普通的非POST版本中对其进行更改,则会使事情搞砸。
这是线路不在其中运行的方法
@bp.route('/livechat', methods=('GET', 'POST'))
def livechat_post():
if request.method == 'POST':
user_id = session.get('user_id')
db = get_db()
if user_id is None:
return redirect(url_for('auth.start_page'))
else:
user = db.execute(
'SELECT * FROM user WHERE id = ?', (user_id,)
).fetchone()
if user is None:
return redirect(url_for('auth.start_page'))
user_details = {
'email': user['email'],
}
print (request.form['flag'], file=sys.stderr)
print ('flag 0', file=sys.stderr)
#the first ajax call for creating the conversation
otherEmail = request.form['otherEmail']
reversed = True
concat_users = user_details['email'] + ":" + otherEmail
concat_reverse = otherEmail + ":" + user_details['email']
#check to see who started the conversation
check_dup = db.execute(
'SELECT * FROM total_msg WHERE identifier = ?', (concat_reverse,)
).fetchone()
if check_dup is None:
reversed = False
db.execute(
'INSERT OR IGNORE INTO total_msg (identifier, total_messages) VALUES (?, ?)', (concat_users, 0,)
)
db.commit()
# the second ajax call for entering a message into the database
if request.form['flag'] == '1':
#adds 1 to the conversation message count and enters the message
print ('flag 1', file=sys.stderr)
if not reversed:
hack = db.execute('SELECT * FROM total_msg WHERE identifier = ?', (concat_users,)).fetchone()
db.execute(
'INSERT OR REPLACE INTO total_msg (identifier, total_messages) VALUES (?, ?)', (concat_users, hack['total_messages'] +1,)
)
db.commit()
# concat_users_msgnumber = concat_users + ":" + str(hack['total_messages'])
db.execute(
'INSERT INTO messages (id, identifier_msg_nmbr, message, sender) VALUES (?, ?, ?, ?)', (hack['total_messages'], concat_users, request.form['the_message'], user_details['email'],)
)
db.commit()
else:
hack2 = db.execute('SELECT * FROM total_msg WHERE identifier = ?', (concat_reverse,)).fetchone()
db.execute(
'INSERT OR REPLACE INTO total_msg (identifier, total_messages) VALUES (?, ?)', (concat_reverse, hack2['total_messages'] +1,)
)
db.commit()
# concat_reverse_msgnumber = concat_reverse + ":" + str(hack2['total_messages'])
db.execute(
'INSERT INTO messages (id, identifier_msg_nmbr, message, sender) VALUES (?, ?, ?, ?)', (hack2['total_messages'], concat_reverse, request.form['the_message'], user_details['email'],)
)
db.commit()
if not reversed:
print ('if', file=sys.stderr)
chat_history = db.execute(
'SELECT * FROM messages WHERE identifier_msg_nmbr = ?', (concat_users,)
).fetchall()
# print (chat_history, file=sys.stderr)
else:
print ('else', file=sys.stderr)
chat_history = db.execute(
'SELECT * FROM messages WHERE identifier_msg_nmbr = ?', (concat_reverse,)
).fetchall()
print (chat_history, file=sys.stderr)
return render_template('main/livechat.html', user=user_details, chat_history=chat_history)
这是即时消息如何使用html中的数据
<div class="row">
<div class="col s4">
<h6 class="bold">messages</h6>
</div>
</div>
{% for item in chat_history %}
<div class="row">
<div class="col s4">
<p>{{item.message}}</p>
</div>
</div>
{% endfor %}
答案 0 :(得分:0)
在烧瓶路径中,如果POST块,所有代码都位于内部。首次加载页面时,方法为GET。没有处理这种情况的例程。
在您的html模板中,没有表单。因此,我想没有办法使用POST提交数据。因此,例程永远不会遇到POST方法。
这全部基于您共享的代码段。
我要这样做的方式如下-
@bp.route('/livechat', methods=('GET', 'POST'))
def livechat_post():
if request.method == 'POST':
#various logic, code here for the cases when method is POST
return redirect(url_for('somepage'))
elif request.method == 'GET':
#codes for the situations when method is GET
#also pass form if form is submitted for the POST method in render_template
#other logics
return render_template('main/livechat.html', user=user_details, chat_history=chat_history)