Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL encoding on Django Template

I have this anchor link:

<a href="/question/tag/1/1/?list_id={{title}}">{{title}}</a> 

Sometimes, this title has some content with + (add operator) like: "Django + Python"

But when it is directly placed on anchor links, the url delivered will be:

http://127.0.0.1:8080/question/tag/1/1/?list_id=Django + Python 

Which will eventually cause problem on the retrieval as the url decoder thought the list_id GET = DjangoPython.

So, does anyone knows how to avoid this issue? Note that I do not want to change anchor links to input buttons.

like image 624
jdtoh Avatar asked Jan 27 '13 12:01

jdtoh


People also ask

How do I encode a URL in Python?

You can encode multiple parameters at once using urllib. parse. urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value.

What is Autoescape in Django?

autoescape. Controls the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. The block is closed with an endautoescape ending tag.

Which characters are illegal in template variable names in Django?

Variable names consist of any combination of alphanumeric characters and the underscore ( "_" ) but may not start with an underscore, and may not be a number.

What does the built in Django template tag Lorem do?

lorem tag displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates.


1 Answers

Instead of

{{ title }} 

do

{{title|urlencode}} 
like image 179
jdtoh Avatar answered Sep 29 '22 22:09

jdtoh