简体   繁体   English

防止Flask在URL中显示'.html'

[英]Prevent Flask from Displaying '.html' in URL

I'm working on a project, using Flask. Apart from the index page, all other pages have '.html' extension displayed at the end of their URL (for example 127.0.0.1:5000/mypage.html ), which is normal.我正在做一个项目,使用 Flask。除了索引页面,所有其他页面在其 URL(例如127.0.0.1:5000/mypage.html )末尾显示“.html”扩展名,这是正常的. For some reasons, i do not want the extension displayed, i prefer the URL displays with the extension (like this 127.0.0.1:5000/mypage . Here's what i have:出于某些原因,我不想显示扩展名,我更喜欢带有扩展名的 URL 显示(例如127.0.0.1:5000/mypage 。这是我所拥有的:

Flask Flask

@app.route('/mypage', methods=['POST', 'GET'])
def mypage():
    return render_template('/mypage.html')

HTML HTML

<a href="mypage.html">Mypage</a></li>

I'll appreciate any help with this.我将不胜感激任何帮助。

Following the documentation: Creating URLs in Flask , you can do this like this, and you are pretty much good to go.按照文档: Creating URLs in Flask ,你可以这样做,并且你对 go 非常好。

Please Note: Do not use / inside the render_template请注意:不要在render_template中使用/

@app.route('/mypage', methods=['POST', 'GET'])
def index():        
    return render_template('mypage.html')

If you want to hardcode the url, you can use url_for() .如果你想硬编码 url,你可以使用url_for() But as the documentation says:但正如文档所说:

Flask can generate URLs using the url_for() function of the flask package. Hardcoding URLs in the templates and view functions is a bad practice . Flask 可以使用 flask package 的url_for() function生成 URL。在模板和视图函数中硬编码 URL 是一种不好的做法

HTML HTML

<a href="/mypage">Mypage</a></li>

I'm working on a project, using Flask.我正在做一个项目,使用 Flask。 Apart from the index page, all other pages have '.html' extension displayed at the end of their URL (for example 127.0.0.1:5000/mypage.html ), which is normal.除索引页面外,所有其他页面的 URL 末尾都显示“.html”扩展名(例如127.0.0.1:5000/mypage.html ),这是正常的。 For some reasons, i do not want the extension displayed, i prefer the URL displays with the extension (like this 127.0.0.1:5000/mypage . Here's what i have:由于某些原因,我不希望显示扩展名,我更喜欢带有扩展名的 URL 显示(例如127.0.0.1:5000/mypage 。这就是我所拥有的:

Flask Flask

@app.route('/mypage', methods=['POST', 'GET'])
def mypage():
    return render_template('/mypage.html')

HTML HTML

<a href="mypage.html">Mypage</a></li>

I'll appreciate any help with this.我会很感激这方面的任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM