python - 在 Flask 中按下提交后我没有得到任何结果
问题描述
我希望当我点击submit
html 时,我的find_winner()
意志会运行并result
显示在我的{{results}}
.
但是当我点击按钮时,什么也没有发生
我的html:
<form method="POST" action="/">
{{ form.submit }}
</form>
<div id=result>
<h3>Computer cards: {{computer_deck}} </h3>
<h3>Your cards: {{user_deck}}</h3>
<h2>Results: {{result}}</h2>
我的 python 和 Flask 部分:
SEQUENCE = ['2', '3', '4', '5', '6', '7', '8', '9', '10',
'J', 'Q', 'K', 'A']
VALUES = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}
SUITS = ['S', 'H', 'C', 'D']
# The computer gets 5 cards
def make_cards(deck = None):
result_deck = set()
while (len(result_deck) < 5):
card = random.choice(SEQUENCE) + random.choice(SUITS)
if (deck != None):
if (card not in deck):
result_deck.add(card)
else:
result_deck.add(card)
return list(result_deck)
def poker_hand_ranking(deck):
hand = deck
suits = sorted([card[-1] for card in hand])
faces = sorted([card[:-1] for card in hand])
if is_royal_flush(hand, suits, faces):
return 1, "You have a Royal Flush"
elif is_straight_flush(hand, suits, faces):
return 2, "You have a Straight Flush"
elif is_four_of_a_kind(hand, suits, faces):
return 3, "You have four of a Kind"
elif is_full_house(hand, suits, faces):
return 4, "You have a full House"
elif is_flush(hand, suits, faces):
return 5, "You have a flush"
elif is_straight(hand, suits, faces):
return 6, "You have a straight"
elif is_three_of_a_kind(hand, suits, faces):
return 7, "You have a three of a Kind"
elif is_two_pairs(hand, suits, faces):
return 8, "You have a two Pair"
elif is_one_pair(hand, suits, faces):
return 9, "You have a pair"
else:
#if none of these counts:
return 10, "High Card"
def find_winner(user_deck, computer_deck):
user_rank = poker_hand_ranking(user_deck)
computer_rank = poker_hand_ranking(computer_deck)
if (user_rank[0] < computer_rank[0]):
print('Winner is user')
elif (user_rank[0] > computer_rank[0]):
print('Winner is computer')
else:
print('Draw!')
print('User:', user_deck, user_rank[1])
print('Computer:', computer_deck, computer_rank[1])
class getresult(FlaskForm):
submit = SubmitField('Click to see the winner')
@app.route("/", methods=['GET', 'POST'])
def homepage():
card_form = getresult()
if card_form.validate_on_submit():
user_deck = make_cards()
computer_deck = make_cards(user_deck)
result = find_winner(user_deck, computer_deck)
return render_template('index.html', form=card_form,
user_deck=user_deck,
computer_deck=computer_deck,
result=result)
return render_template('index.html', form=card_form)
if __name__ == '__main__':
# print(poker_hand_ranking(["10H", "JH", "QH", "AH", "KH"]))
app.run(debug=True)
解决方案
需要 HTML
{{ form.csrf_token }}
{{ form.submit }}
推荐阅读
- ios - 有没有办法清除 iOS 上个人热点的 DHCP 租约?
- mongodb - mongodb - 副本集创建错误:仲裁检查失败,因为没有足够的投票节点响应
- c++ - 等待串口数据并合并结果
- python - 是否有任何优雅的方法来定义具有 dtype 数组列的数据框?
- python - 如何在 Python 中跟踪图像中的曲线?
- vuejs2 - 如何在RouteUpdate导航守卫之前测试vue路由器?
- php - Laravel:会话变量或多次设置变量?
- c - 附加函数中的 C 指针未更新头尾的值
- android - 提取pdf文件文本并在应用程序上显示
- c - 如何使用字符串声明作为实际成员的表示来调用结构的成员?