。在如下圖所示添加破折號我想在我的網址添加幾許瓶時自動生成的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 %}
你想創建一個'POST'紀錄標題**「這是標題」 ** **或「this_is_a_title」 **?你如何發送帖子請求?提交表單?使用HTTP客戶端請求,郵遞員?一般來說,當URL包含空格時,空格在html charset中被替換爲%20;即Chrome會自動替換空格。通常情況下,你不需要擔心它。 – ohannes
我通過提交表單發送發佈請求,我不想更改我的標題,所以我想出了這個問題。正如你所說,但Firefox沒有將'空間'改爲'%20' – simp1e