简体   繁体   English

无法使用 url_for flask、python、html5 传递变量

[英]cannot pass variable with url_for flask, python, html5

I was trying to put a link in some text through url_for (flask, python3) and also pass a variable but nothing I tried worked .Here's the code I used.我试图通过 url_for (flask, python3) 在一些文本中放置一个链接,并传递一个变量,但我尝试过的没有任何效果。这是我使用的代码。 Am I doing something wrong?难道我做错了什么? html5: html5:

<a class="mr-2" href={{ url_for('user','username'=posts.author)}} >TEXT</a>

python3:蟒蛇3:

@app.route('/user')
def user(username):
    print(username)

You'd need to change the template code to look like:您需要将模板代码更改为如下所示:

<a class="mr-2" href="{{ url_for('user',username=posts.author) }}" >TEXT</a>

However you'd also have to change the python code for this to be valid:但是,您还必须更改 python 代码才能使其有效:

@app.route('/user/<username>')
def user(username):
    print(username)

A valid request is now:一个有效的请求现在是:

/user/CodEdo

Which is the URL which should be rendered in the href assuming that posts.author in the template is CodEdo .这是应该在呈现的URL href假设posts.author在模板CodEdo

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

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