Python 如何创建指向另一个 html 页面的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27539309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a link to another html page?
提问by Richael
I have a form on one page that I want to submit to another page. I can't figure out how to create the link to that second page.
我在一个页面上有一个表单,我想提交到另一个页面。我不知道如何创建到第二页的链接。
Project layout:
项目布局:
Fileserver/
config.py
requirements.txt
run.py
setup.py
app/
__init__.py
static/
css/
img/
js/
templates/
formAction.html
formSubmit.html
index.html
__init__.py
:
__init__.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
ip = request.remote_addr
return render_template('index.html', user_ip=ip)
index.html
:
index.html
:
<!DOCTYPE html>
<html lang="en">
<body>
<ul>
<li><a href="/formSubmit.html">Check Out This Form!</a>
</ul>
</body>
</html>
I can see the page at localhost:5000/ without issue.
我可以在 localhost:5000/ 看到页面,没有问题。
I have also tried:
我也试过:
<a href="{{ url_for('templates', 'formSubmit") }}"></a>
as well as:
也:
<a href="{{ url_for('formSubmit') }}"></a>
What am I missing?
我错过了什么?
采纳答案by davidism
url_for
generates urls to routes defined in your application. There are no (or should probably not be any) raw html files being served, especially out of the templates folder. Each template should be something rendered by Jinja. Each location you want to display or post a form to should be handled and generated by a route on your application.
url_for
生成应用程序中定义的路由的 URL。没有(或可能没有)原始 html 文件被提供,尤其是在模板文件夹之外。每个模板都应该是 Jinja 渲染的东西。您想要显示或发布表单的每个位置都应由应用程序上的路由处理和生成。
In this case, you probably want to have one route to both render the form on GET and handle the form submit on POST.
在这种情况下,您可能希望有一种方法既可以在 GET 上呈现表单,又可以在 POST 上处理表单提交。
__init__.py
:
__init__.py
:
from flask import Flask, request, url_for, redirect, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/cool_form', methods=['GET', 'POST'])
def cool_form():
if request.method == 'POST':
# do stuff when the form is submitted
# redirect to end the POST handling
# the redirect can be to the same route or somewhere else
return redirect(url_for('index'))
# show the form, it wasn't submitted
return render_template('cool_form.html')
templates/index.html
:
templates/index.html
:
<!doctype html>
<html>
<body>
<p><a href="{{ url_for('cool_form') }}">Check out this cool form!</a></p>
</body>
</html>
templates/cool_form.html
:
templates/cool_form.html
:
<!doctype html>
<html>
<body>
<form method="post">
<button type="submit">Do it!</button>
</form>
</html>
I don't know what your forms and routes actually do, so this is just an example.
我不知道你的表单和路由实际上是做什么的,所以这只是一个例子。
If you need to link static files, put them in the static
folder, then use:
如果需要链接静态文件,将它们放在static
文件夹中,然后使用:
url_for('static', filename='a_picture.png')