Flask 可选路由在 URL 中添加 /None/None

发布于 2025-01-09 09:02:47 字数 839 浏览 0 评论 0原文

我一直在尝试设置一些可选参数,如下所示。用户应该能够浏览层次结构中任意位置的前 100 名、州或城市。

@app.route("/guide", defaults={'state': None, 'city': None})
@app.route("/guide/<state>", defaults={'state': None})
@app.route("/guide/<state>/<city>")
def guide_route(state, city):
    if state == 'top_100':
        return render_template('top_100.html')
    elif state:
        return render_template('state.html', data={'state': state})
    elif state and city:
        return render_template('city.html', data={'state': state, 'city': city})
    else:
        return render_template('something_else.html')

但是,当我在网络浏览器中访问 /guide/top_100/guide 时,它会将我重定向到 /guide/None/None是 404。同样有趣的是,我现在已将 print 语句放入 guide_route() 函数的每个部分中,但它们都没有触发。所以路由表中的某些东西根本不起作用。

如何让这些可选参数发挥作用?

I've been trying to set up some optional parameters like below. The user should be able to browse either the top 100, the states or the cities at any point in the heirarchy.

@app.route("/guide", defaults={'state': None, 'city': None})
@app.route("/guide/<state>", defaults={'state': None})
@app.route("/guide/<state>/<city>")
def guide_route(state, city):
    if state == 'top_100':
        return render_template('top_100.html')
    elif state:
        return render_template('state.html', data={'state': state})
    elif state and city:
        return render_template('city.html', data={'state': state, 'city': city})
    else:
        return render_template('something_else.html')

However, when I go to /guide/top_100 or /guide in my web browser, it redirects me to /guide/None/None which is a 404. What's also interesting is that I've now put print statements inside every part of the guide_route() function and none of them fire. So something isn't working in the routing table at all.

How can I get these optional parameters to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-01-16 09:02:47

您输入默认值的方式有误。您必须在函数定义中执行此操作(而不是在路由中):

@app.route("/guide",)
@app.route("/guide/<string:state>")
@app.route("/guide/<string:state>/<string:city>")
def guide_route(state=None, city=None):
    if state == 'top_100':
        return render_template('top_100.html')
    elif state and city:
        return render_template('city.html', data={'state': state, 'city': city})
    elif state:
        return render_template('state.html', data={'state': state})
    else:
        return render_template('something_else.html')

https://flask.palletsprojects.com/en/2.0.x/quickstart/#variable-rules

具有默认值的示例: https://flask.palletsprojects.com /en/2.0.x/quickstart/#rendering-templates

(我还颠倒了两个 elif 条件)

There is a mistake in the way you input the default values. You have to do it in the function definition (and not in routes) :

@app.route("/guide",)
@app.route("/guide/<string:state>")
@app.route("/guide/<string:state>/<string:city>")
def guide_route(state=None, city=None):
    if state == 'top_100':
        return render_template('top_100.html')
    elif state and city:
        return render_template('city.html', data={'state': state, 'city': city})
    elif state:
        return render_template('state.html', data={'state': state})
    else:
        return render_template('something_else.html')

https://flask.palletsprojects.com/en/2.0.x/quickstart/#variable-rules.

An example with default value : https://flask.palletsprojects.com/en/2.0.x/quickstart/#rendering-templates

(I also inverted two elif conditions)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文