首页 > 解决方案 > 无法将 url 给定的变量传递给路由的回调函数和重定向问题

问题描述

烧瓶应用程序.py

@app.route( '/' ) 
@app.route( '/<page>' ) 
def index( page ): 

# use the variable form template for displaying 
counter = '''<center> 
                <table bgcolor=black bordercolor=orangered> 
                td><font size=3 color=lime> Αριθμός Επισκεπτών: </font></td> 
<td><a href="{{ url_for( '/log', page='%s' ) }}"><font size=3 color=plum> %d </font></a></td> 
                                </table> 
                        ''' % (page, pagehit) 

if page != 'index.html': 
    pdata = redirect( 'http://superhost.gr/cgi-bin/' + page ) 
    return pdata 

模板 index.html

<div align=right> 
        <a href="{{ url_for( '/', page='some_value' ) }}">        <img src="/static/images/π.gif"> </a> 
</div> 

<a href="{{ url_for( '/', page='another_value' ) }}">        <img src="/static/images/download.gif"></a> 


我要做的就是当用户单击 html 模板中的图像时,将这些变量值传递给 '/' 路由,以便使用这些值执行一些操作

我收到的错误是这样的:

builtins.TypeError 
TypeError: index() missing 1 required positional argument: 'page'

我的意思是我确实已经在路由中声明并且也确实有'page'作为回调函数的变量,为什么它看不到它?

  1. 还有关于我正在使用的重定向功能......有没有办法取回运行该 cgi 脚本的 HTML 响应,然后向该响应添加另一个 HTML 值?我使用了 subprocess 和 Response 但他们无法将内容传递回 pdata 变量以进行显示。除其他外,我真正想做的是从我的烧瓶应用程序脚本中尝试运行并获取 cgi 脚本的响应

我刚刚试过这个:

@app.route( '/' )
@app.route( '/<page>' )
def index( page='index.html' ):

但我得到:

werkzeug.routing.BuildError
werkzeug.routing.BuildError: Could not build url for endpoint '/' with values ['page']. Did you mean 'log' instead?

我希望 'page' 变量具有默认值 'index.html' 但我想知道为什么它不能运行它。'page=None' 也不起作用。

标签: pythonflask

解决方案


推荐阅读