Python瓶子-“redirect”和“return template”之间的区别

2024-04-19 13:59:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个关于瓶子的问题:

1)以下各项之间的区别是什么:

redirect('/login')return template('login')

难道这两者都不能让用户在同一个/login页面上运行吗?

2)我是否可以像在return的情况下那样将参数传递给redirect

例如:

这是否有效:redirect('/login', userName="foo")就像我们在本例中所做的那样:

return template('login', userName="foo")


Tags: 用户瓶子returnfoousername情况logintemplate
1条回答
网友
1楼 · 发布于 2024-04-19 13:59:52

1) What is the difference between:

redirect('/login') and return template('login')

bottle documentation for redirect

To redirect a client to a different URL, you can send a 303 See Other response with the Location header set to the new URL. redirect() does that for you

redirect()方法将向用户发送303响应,然后用户将向您的服务器发送另一个有关'/login'页的请求。如果使用template()方法,则将直接将网页返回给用户。

2) Can I pass arguments to redirect as I do in case of return?

redirect()不接受查询变量,例如传递给template()的变量。如果要使用这些变量,则需要在url上显式设置它们。E、 g.要将url '/login'userName="foo"一起使用,需要调用redirect('/login?userName="foo")

编辑如果不想在url中存储所有变量,则应在呈现页面时尝试获取这些值。 e、 g.在不使用变量的情况下调用redirect('/login'),并使呈现“/login”的方法负责使用正确的变量调用template()

相关问题 更多 >