添加破折号我想在我的网址添加几许瓶时自动生成的URL网址

问题描述:

。在如下图所示添加破折号我想在我的网址添加几许瓶时自动生成的URL网址

代码:

@main.route('/post_detail/<string:title>', methods=['GET', 'POST']) 
def post_detail(title): 
    post = Post.query.filter_by(title=title).first_or_404() 
    return render_template('post_detail.html', post=post) 

由于我使用FLASK内置转换的途径,当出现在标题空间(例如,标题为title title),该网址会是这样xxx.title title,我应该怎么做才能在网址中添加短划线,如xxx.title_title

而且我不希望在我的标题加破折号(例如,标题为title_title

这里是我的模板:

post_detail.html

<h1 color='black'>{{ post.title | replace('_', ' ') }}</h1> 
<h2 class="subheading">{{ post.summary }}</h2> 
<span class="meta">Posted by <a href="#">{{ post.author.username }}</a></span> 

`和post_list.html

{% for post in posts %} 
<div class="post-preview"> 
    <a href="{{ url_for('main.post_detail', title=post.title) }}"> 
     <h2 class="post-title">{{ post.title }}</h2> 
     <h3 class="post-subtitle">{{ post.summary }}</h3> 
    </a> 
    <p class="post-meta">Posted by <a href="#">{{ post.author.username }}</a></p> 
</div> 
{% endfor %} 
+0

你想创建一个'POST'纪录标题**“这是标题” ** **或“this_is_a_title” **?你如何发送帖子请求?提交表单?使用HTTP客户端请求,邮递员?一般来说,当URL包含空格时,空格在html charset中被替换为%20;即Chrome会自动替换空格。通常情况下,你不需要担心它。 – ohannes

+0

我通过提交表单发送发布请求,我不想更改我的标题,所以我想出了这个问题。正如你所说,但Firefox没有将'空间'改为'%20' – simp1e

很高兴看到您的模板。

无论如何,这里是你的问题的一个可行的解决方案。

重要的是你如何构造的细节网址模板。

url_for使用一个参数。另外,当您显示帖子的详细信息页面(包括其标题中的空格)时,请检查浏览器地址栏。

所有的html charset替换将由模板引擎执行,所以你不必担心它。

在您的实现,您可以根据您可能正在使用SQLAlchemy的ORM删除get_post方法和POSTS列表。他们只是为了快速测试。

app.py

from flask import Flask, abort, render_template 

main = Flask(__name__) 

POSTS = [ 
    {'title': 'this_is_a_title'}, 
    {'title': 'this_is_another_title'}, 
    {'title': 'this title contains space'}, 
] 

def get_post(title): 
    for post in POSTS: 
     if post['title'] == title: 
      return post 
    return None 

@main.route('/post_list', methods=['GET', 'POST']) 
def post_list(): 
    return render_template('post_list.html', posts=POSTS) 

@main.route('/post_detail/<string:title>', methods=['GET', 'POST']) 
def post_detail(title): 
    #post = Post.query.filter_by(title=title).first_or_404() 
    post = get_post(title) 
    if post is None: 
     abort(404) 
    return render_template('post_detail.html', post=post) 

if __name__ == '__main__': 
    main.run(debug=True) 

模板/ post_list.html

<html> 
    <body> 
    <h1>POST LIST</h1> 
    {% for post in posts %} 
     <h3>{{ post.title }}</h3> 
     <a href="{{ url_for('post_detail', title=post.title) }}">details</a> 
    {% endfor %} 
    </body> 
</html> 

模板/ post_detail.html

<html> 
    <body> 
    <h1>POST DETAIL</h1> 
    <h3>{{ post.title }}</h3> 
    </body> 
</html> 

合e,它有帮助。

+0

感谢您的帮助。但我认为你不明白或者让你困惑。我想要的是在URL中将'space'变成'_',而不在参数'title'中输入,我不想在标题中添加短划线。 – simp1e

+0

是的,我真的很困惑。谁在您的网址中添加破折号?为什么你需要用下划线替换空格?而且,如何通过标题用下划线过滤记录,而其原始形式是否包含数据库中的空格?如果您可以展示您的相关模板,我会尽力理解并帮助您。 – ohannes

+0

因为我觉得很难用空格填充地址,所以我尝试用下划线替换空格。根据你的建议,我想出了一个想法,即将模板中的下划线替换为空格,(Jinja)模板为'

{{post.title |替换('_','')}}

'。但我必须在标题中加下划线。这只是一种折衷方法。但是,无论如何,非常感谢:) – simp1e