urlencode Filter

URL Most Useful

Argument: s (Optional) – a string containing characters that should not be escaped.

Documentation

Escapes a string for use in a URL.

Variable

url = 'https://www.webucator.com/search/search.cfm?q=django'

Template

{{ url|urlencode }}

Result

https%3A//www.webucator.com/search/search.cfm%3Fq%3Ddjango

Forward slashes (/) are not escaped by default. To escape all special characters, pass an empty string ("") for the argument.

Additional Examples

{{ url|urlencode:"/" }}
{{ url|urlencode:"" }}
{{ url|urlencode:":" }}

Results

https%3A//www.webucator.com/search/search.cfm%3Fq%3Ddjango
https%3A%2F%2Fwww.webucator.com%2Fsearch%2Fsearch.cfm%3Fq%3Ddjango
https:%2F%2Fwww.webucator.com%2Fsearch%2Fsearch.cfm%3Fq%3Ddjango

Commentary

The urlencode filter can be used for passing URL parameters back to the querystring in generated links. For example:

<a href="?page=1&ordering={{ request.GET.ordering|urlencode }}">1</a>

Did we get something wrong? Is there a use case for the urlencode filter that we should add? Please let us know.

Send Feedback

Official Documentation
This page last updated on Oct. 30, 2022, 1:21 p.m. EST