スペース、タブ、改行、!、@、#、$、%、&、(、)、.、をダッシュに置き換えるカスタム テンプレート タグがあります。
re.sub('[,@#$%&_.?!\t\n ]+', '-', value)
私が与えるとき、これはうまくいきますvalue parameter explicitly
:
re.sub('[,@#$%&_.?!\t\n ]+', '-', 'نمونه کد')
また:
value='نمونه کد'
re.sub('[,@#$%&_.?!\t\n ]+', '-', value)
しかし、テンプレートでは、オブジェクトのリストのフィールドでこのタグを使用したい場合、subject
正しく機能せず、スペースをダッシュに置き換えるだけです:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews n.subject|custom_unicode_slugify,n.pk %}" >{{n.description|safe|truncatewords_html:15}}</a>
{% endfor %}
これは私のカスタム テンプレート タグです。
def custom_unicode_slugify(value):
return re.sub('[,@#$%&_.?!\t\n ]+', '-', value)
register.filter('custom_unicode_slugify', custom_unicode_slugify)
モデルメソッドが を返すため、n|custom_unicode_slugify
の代わりにこのタグを使用しようとしましたが、次のエラーが発生します。n.subject|custom_unicode_slugify
__unicode__()
subject field
Caught TypeError while rendering: expected string or buffer