user2387135
user2387135

Reputation: 99

how to change unicode to string in django template

I am getting NoReverseMatch because I'm getting a unicode instead of a string, is there a way I could turn unicode to string in the templates?

this is my normal hyperlink

# hyperlink in templates
    {% for lst in mylist %}
        <a href="{% url "url_list" lst.user.username %}"> {{ lst.user.username }} </a>
    {% endfor %}

how can I do lst.user.username.encode("utf-8") inside a hyperlink?

# 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'),

Edit:

I changed the regex in my url and it's working now.

url(r'^(?P<username>[\w.@+-]+)/list/$', url_list.as_view(), name='url_list'),

Upvotes: 0

Views: 958

Answers (1)

lucemia
lucemia

Reputation: 6627

The django will render unicode string to utf-8 automatically.

maybe the issue is the url routing? try to add ur before the regex patterns

url(ur'^(?P<username>[-\w]+)/list/$', url_list.as_view(), name='url_list'),

Upvotes: 1

Related Questions