无法使用值 ['type'] 为终结点'topic'生成 url。你的意思是'topic_func'吗?



>我正在尝试在 jinja 中创建动态 url,我需要将所选值从 html 页面传递到烧瓶以创建新网页。

我在下拉菜单中有主题,当我单击链接时,需要将信息传递给烧瓶,该链接已选择以及该页面上将显示的内容,但发生错误,指出无法为 url 构建端点。任何帮助将不胜感激。提前感谢!!

dashboard.py 文件在这里:

@app.route('/')
def home():
#topic is a list of topics for the dropdown menu
return render_template("home.html",topic=topics)
@app.route('/topic', methods=['GET', 'POST'])
def topic_func():
result = request.args.get('type')
print(result) #should print what was the topic that was clicked in the dropdown menu 
return render_template(index.html, value=result)

这是家.html我也无法获得类型的值。当它应该打印所选主题的值时,它正在打印 {{each}}。我该如何解决这个问题?

<div class="dropdown-content">
{% for each in topics %}
<a href="{{url_for('topic', type='{{each}}')}}">{{each}}</a>
{% endfor %}
</div>

你应该像这样构建你的代码:

<div class="dropdown-content">
{% for each in topics %}
<a href="{{url_for('topic_func', type='')}}{{each}}">{{each}}</a>
{% endfor %}
</div>

备注两件事:

  1. url_for年,我将路由topic替换为函数topic_func。这是因为url_for寻找一个函数。所以你必须给它传递一个函数的名称

  2. 我将值移动到发送{{each}},在url_for的两个大括号之后。原因是将它们留在引号内,函数url_for不会将其理解为 jinja 变量,但它认为它是您要发送的确切值。所以你会有,在蟒蛇方面:

蟒:

@app.route('/topic', methods=['GET', 'POST']) 
def topic_func():
result = request.args.get('type')
print(result)  # {{each}}

最新更新