【发布时间】:2014-06-19 17:41:59
【问题描述】:
我得到 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'),
【问题讨论】:
-
Unicode 绝对不是这里的问题。您应该发布确切的错误消息和回溯。
标签: python django django-templates django-urls