Python 如何使用flask在index.html上创建一个非常简单的超链接?
以下是我的结构:Python 如何使用flask在index.html上创建一个非常简单的超链接?,python,html,flask,Python,Html,Flask,以下是我的结构: ├── main.py ├── static │ ├── css │ │ └── main.css │ ├── img │ │ └── main_bkg.jpg │ └── js └── templates ├── myApp │ └── myApp.main.html └── index.html 我只想在index.html上创建一个非常简单的超链接,链接到myApp.main.html 我的main.py看起来像:
├── main.py
├── static
│ ├── css
│ │ └── main.css
│ ├── img
│ │ └── main_bkg.jpg
│ └── js
└── templates
├── myApp
│ └── myApp.main.html
└── index.html
我只想在index.html
上创建一个非常简单的超链接,链接到myApp.main.html
我的main.py
看起来像:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<module>/')
def myApp(module):
return render_template('{0}/{0}.main.html'.format(module))
@app.route('/user/<username>')
def show_user_profile(username):
return 'User %s' % username
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
烧瓶提示BuildErrorBuildError:('myApp',{},None)
如果我在myApp.main.html
上尝试以下操作,它将提供一个返回主页的有效链接:
<a href="{{ url_for('index') }}"><button>Home</button></a>
我在这里遗漏了什么?您的
myApp
规则采用了模块
参数。您没有将该信息传递给的url\u,因此它引发了一个错误,因为它需要该信息来构建url
url_for('myApp', module='myModule')
我使用了以下结构:
├── app.py
├── 静止的
└── 模板
├── Homep.html
└── index.html
我使用了HTML:
这对我来说很好。您是否尝试过通过myApp
路径返回静态HTML文件?在构建时,当装饰符号被传输到应用程序路由时,可能会引发错误。返回一个静态HTML文件甚至字符串将有助于确定错误是来自HTML文件还是路由本身。
url_for('myApp', module='myModule')