【发布时间】:2020-05-17 15:46:53
【问题描述】:
我是一名 Python 初学者,我正在尝试建立一个网站,该网站有一个按钮,每次点击按钮都会弹出一个新窗口,其中包含随机的 Flash 游戏。
现在,我的按钮可以工作了,但它总是打开同一个 Flash 游戏,只有当我刷新页面并再次点击时,它才会加载一个新游戏。
有人可以向我解释发生了什么问题,我该如何解决这个问题? :)
在此之前,我抓取了一篇文章,其中包含大约 150 个 Flash 游戏的链接,这些游戏现在位于 .txt 文件中,例如:
http://www.tapeonline.com/snowball/
http://www.gamesforwork.com/games/play-4626-Necrathalon-Flash_Game
http://www.adobe.com/macromedia/holiday2004/
http://www.kongregate.com/games/arawkins/dolphin-olympics-2
这是我的代码:
烧瓶
from flask import Flask, render_template, request, redirect
import random
app = Flask(__name__)
@app.route("/" , methods=['POST','GET'])
def home():
if request.method == 'POST':
return redirect("/")
else:
a = open('gamelinks.txt','r')
b = a.read().split()
a.close()
return render_template('index.html', game=random.choice(b))
if __name__ == '__main__':
app.run(debug=True)
html:
{% extends "base.html" %}
{%block head %}
{% endblock %}
<title>FLASHER</title>
{% block body %}
<form action = '{{ game }}' method='POST' target="_blank">
<input type='submit', name='submit', id='submit', value='Play Game'>
</form>
{% endblock %}
【问题讨论】:
标签: python html python-3.x web flask