Create hyperlink in django template of object that has a space

django, html, hyperlink, templates

Solution

Use the `urlencode` filter.

{{ item|urlencode }}

But why are you taking the name? You should be passing the appropriate view and PK or slug to `url` which will create a suitable URL on its own.

Problem

I am trying to create a dynamic hyperlink that depends on a value passed from a function: ``` {% for item in field_list %} <a href={% url index_view %}{{ item }}/> {{ item }} </a> <br> {% endfor %} ``` The problem is that one of the items in field_list is "Hockey Player". The link for some reason is dropping everything after the space, so it creates the hyperlink on the entire "Hockey Player", but the address is ``` http://126.0.0.1:8000/Hockey ``` How can I get it to go to ``` http://126.0.0.1:8000/Hockey Player/ ``` instead?

Original source