且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Flask中,如何在单击按钮时生成动态URL?

更新时间:2022-04-29 01:46:04

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

您可以在REPL上对其进行测试:

You can test it on a REPL:

import flask
app = flask.Flask(__name__)

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

ctx = app.test_request_context()
ctx.push()

flask.url_for('.profile', user=1)
'/profile1'

如何将 user 参数传递到新路由取决于您的需求.如果您需要 profile1 profile2 的硬编码路由,则可以分别传递 user = 1 user = 2 .如果要以编程方式生成这些链接,请取决于这些配置文件的存储方式.

how you pass the user parameter to your new route depends on what you need. If you need hardcoded routes for profile1 and profile2 you can pass user=1 and user=2 respectively. If you want to generate those links programatically, depends on how these profiles are stored.

否则,您可以将请求对象中已解析的元素重定向而不是 render_template 重定向到 url_for .这意味着有两条路线

Otherwise you could redirect instead of render_template, to the url_for with the parsed element in the request object. This means having two routes

@app.route('/profile<int:user>')
def profile_pretty(user):
    print(user)

@app.route('/profile', methods=['POST'])
def getProfile():
      if request.form['submit'] = 'profile1':
           return redirect(url_for('.profile_pretty', user=1))
       else if request.form['submit'] = 'profile2':
            return redirect(url_for('.profile_pretty', user=2))

小凹坑:这会使您的路线看起来像您想要的那样,但这效率低下,因为它每次都会生成一个新请求,只是以您想要的方式生成网址.此时,可以安全地问为什么您要动态生成静态内容的路由.

caveat: This would make your routes look like you want, but this is inefficient as it generates a new request each time, just to make your urls the way you want. At this point it's safe to ask why do you want to have dynamically generated routes for static content.


http://exploreflask.com/en/latest/views.html#url-converters

在Flask中定义路由时,可以指定要转换为Python变量并传递给视图函数的部分.

When you define a route in Flask, you can specify parts of it that will be converted into Python variables and passed to the view function.

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

标有URL的部分中的任何内容都将作为用户名参数传递给视图.您还可以指定一个转换器,以在将变量传递到视图之前对其进行过滤.

Whatever is in the part of the URL labeled will get passed to the view as the username argument. You can also specify a converter to filter the variable before it’s passed to the view.

@app.route('/user/id/<int:user_id>')
def profile(user_id):
    pass

在此代码块中,URL http://myapp.com/user/id/Q29kZUxlc3NvbiEh 将返回404状态代码-找不到.这是因为URL的应该是整数的部分实际上是一个字符串.

In this code block, the URL http://myapp.com/user/id/Q29kZUxlc3NvbiEh will return a 404 status code – not found. This is because the part of the URL that is supposed to be an integer is actually a string.

我们还可以使用第二个视图来查找字符串.将会为/user/id/Q29kZUxlc3NvbiEh/调用,而第一个会为/user/id/124调用.

We could have a second view that looks for a string as well. That would be called for /user/id/Q29kZUxlc3NvbiEh/ while the first would be called for /user/id/124.