python - 无法将 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'作为回调函数的变量,为什么它看不到它?
- 还有关于我正在使用的重定向功能......有没有办法取回运行该 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' 也不起作用。
解决方案
推荐阅读
- javascript - 选项之间的数据传输到相同的值和值
- algorithm - 图中具有特定长度的顶点不相交路径
- ssl - 使用 Globalsign R2 根证书的 Google Cloud 连接设备
- python - 使用python拆分行并将分隔符添加到文本文件
- python - 将字典的第一项作为Python中的字典返回?
- python-3.x - python在文档字符串中自动添加类型提示
- javascript - 从范围列表中大写
- dataframe - PySpark 动态爆炸嵌套列 - 从使用 FOR 更改为更优化的解决方案
- mysql - MySQL 日志记录使用触发器更改。有没有更好的办法?
- python - 给定一个排序数字列表,根据数字之间的距离划分为子列表