简体   繁体   English

Python、Flask - 如何在模板文件夹中创建可点击的链接并重定向到不同的页面?

[英]Python, Flask - How do I make clickable links in the templates folder and redirect to a different page?

This is my flask.py file:这是我的 flask.py 文件:

names = ["jack", "bob", "sam"]

@app.route('/home')
def home():
    return render_template('home.html', names=names)

@app.route('/name')
def name():
    return render_template('names.html')

and this is my home.html file:这是我的家。html 文件:

{% for row in names %}
    <a> {{row}} </a>        
{% endfor %}

What I am trying to do is, make each {{row}} tag a clickable link and have it redirect me to the names.html file.我想要做的是,让每个 {{row}} 标记成为可点击的链接,并让它将我重定向到 names.html 文件。

I want the names to have their own unique page and perhaps a way to store the row name that I click in a variable and somehow send it to the flask.py file.我希望这些名称有自己独特的页面,也许还有一种方法来存储我在变量中单击的行名,并以某种方式将其发送到 flask.py 文件。

{% for row in names %}
<a href="name/{{row.name}}"> {{row}} </a>        
 {% endfor %}

Jinja allows for dynamic variable naming which also means dynamic path links. Jinja 允许动态变量命名,这也意味着动态路径链接。

So if you wanted each name to take you to a special route you would need to first create a route that will display unique information..因此,如果您希望每个名称都将您带到一条特殊的路线,您需要首先创建一条显示独特信息的路线。

Now if you want your web server to receive data from the html file you will need to create a route that takes in a POST request and give it the logic as to what to do with the data.现在,如果您希望 web 服务器从 html 文件接收数据,您将需要创建一个接收 POST 请求的路由,并为其提供有关如何处理数据的逻辑。 Weather that me to redirect to index and pass in the data names=names... etc我重定向到索引并传入数据名称=名称...等的天气

Let me know if this helped you out at all or if there is anything that I can help clear up!让我知道这是否对您有所帮助,或者是否有任何我可以帮助您解决的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM