[英]how to change unicode to string in django template
我收到NoReverseMatch的原因是我得到的是Unicode而不是字符串,是否可以在模板中将Unicode转换为字符串?
这是我的普通超链接
# hyperlink in templates
{% for lst in mylist %}
<a href="{% url "url_list" lst.user.username %}"> {{ lst.user.username }} </a>
{% endfor %}
如何在超链接内执行lst.user.username.encode(“ utf-8”)?
# hyperlink in templates
{% for lst in mylist %}
<a href="{% url "url_list" lst.user.username.encode("utf-8") %}"> {{ lst.user.username }} </a>
{% endfor %}
# Url
url(r'^(?P<username>[-\w]+)/list/$', url_list.as_view(), name='url_list'),
编辑:
我在网址中更改了正则表达式,现在可以正常使用了。
url(r'^(?P<username>[\w.@+-]+)/list/$', url_list.as_view(), name='url_list'),
django将自动将unicode字符串呈现为utf-8。
也许问题是网址路由? 尝试在正则表达式模式之前添加ur
url(ur'^(?P<username>[-\w]+)/list/$', url_list.as_view(), name='url_list'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.