Pea*_*ady 6 html python button flask
在我的主页上有一个按钮,上面写着“Enter”
<button id="Enter" type="button" onclick="/profile" class="button">Enter</button>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望这样当单击此按钮时它会定向到“主页”页面
Python代码:
@app.route('/', methods = ['GET', 'POST'])
def index():
return render_template("main.html")
@app.route('/home', methods = ['GET', 'POST'])
def home():
return render_template("test1.html")
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?谢谢
这是锚标签的工作。
Jinja2(Flask 的模板渲染组件)允许您使用该url_for函数为给定的视图函数动态创建 url。
在你的情况下:
<a href="{{ url_for('home') }}">Home</a>
Run Code Online (Sandbox Code Playgroud)
使用您的代码,这将在浏览器中呈现为:
<a href="/home">Home</a>
Run Code Online (Sandbox Code Playgroud)
即使您更改app.route装饰器函数中的端点,也会使用正确的 URL 进行渲染。
为了使其具有按钮的外观,我建议使用 Bootstrap 库。head通过在HTML 模板中添加以下内容,可以将其包含在 CDN 中:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" integrity="sha256-/ykJw/wDxMa0AQhHDYfuMEwVb4JHMx9h4jD4XvHqVzU=" crossorigin="anonymous" />
Run Code Online (Sandbox Code Playgroud)
然后分配按钮样式就像class向锚标记添加属性一样简单:
class='btn btn-primary'
Run Code Online (Sandbox Code Playgroud)
请参阅引导文档中的其他可用类