Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create hyperlink in django template of object that has a space

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?

like image 738
Ed. Avatar asked Dec 18 '22 02:12

Ed.


1 Answers

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.

like image 98
Ignacio Vazquez-Abrams Avatar answered Jan 04 '23 23:01

Ignacio Vazquez-Abrams