如何從django模板中的字符串中刪除空格

[英]How to remove spaces from string in django template


How do you remove all spaces from a string value in a django template?

如何從django模板中的字符串值中刪除所有空格?

For example

例如

<a href="www.somesite.com/lookup?id={{ order.id }}"</a>

The order number may have spaces in it and how the templates encodes them breaks the lookup function of the site. (Not our site so can't fix that end)

訂單號中可能包含空格以及模板如何對其進行編碼會破壞站點的查找功能。 (不是我們的網站所以無法解決這個問題)

I'm aware that you can use order.id.strip to get rid of the spaces at the front and end but we need to remove them from the middle of the string as well.

我知道你可以使用order.id.strip去除前端和后端的空格,但我們也需要從字符串的中間刪除它們。

I'm also aware that we could create a custom filter to do it but we like to avoid custom filters for one-off things like this.

我也知道我們可以創建一個自定義過濾器來執行此操作,但我們希望避免使用自定義過濾器來實現這樣的一次性過濾。

1 个解决方案

#1


15  

From: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#cut

來自:https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#cut

cut

Removes all values of arg from the given string.

從給定的字符串中刪除arg的所有值。

For example:

例如:

{{ value|cut:" " }}

{{value | cut:“”}}

If value is "String with spaces", the output will be "Stringwithspaces".

如果value是“帶空格的字符串”,則輸出將為“Stringwithspaces”。

This worked perfectly for what I wanted. Was not easy to find so creating this question to help the google juice for the next person.

這完全符合我的要求。不容易找到這樣創建這個問題,以幫助谷歌果汁為下一個人。

So in my example it would be:

所以在我的例子中它將是:

<a href="www.somesite.com/lookup?id={{ order.id|cut:" " }}"</a>

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/06/30/215a0b06ba95094d823320b918c30211.html



 
  © 2014-2022 ITdaan.com 联系我们: