Search code examples
pythonhtmlunicodeencodewebapp2

Get data from href but it is not usable


Recently i started working on a project. In which i am sending data from a html page using <a> tag. At backend i am using python webapp2 feamework. When i get the data it show perfectly. But when i compare it with some string for further usage it does not work.

I know when we get data it is in unicode. But i converted it to utf-8 and it is still not working.

Here is the code in html. Suppose i sent "item 2" as itemname

<a href="/main/items/?itemname= {{res.itemName}}&itemdescription={{res.itemDescription}}"> Click me </a>

The code which i am using to fetch data is

def get(self,nam,des):

   nam = self.request.get('itemname')
   itemDesc= self.request.get('itemdescription')
   name = nam.encode('utf-8')

   if name == "item 2":
       self.response.write("Equal")

I also try it without encoding but still it not works. It Show the value of item name perfectly. But it is not comparing them. Please help where i am doing mistake.


Solution

  • Looks like 2 issues, there is a space before the expected value is set, and the string is urlquoted.

    To fix the space:

    <a href="/main/items/?itemname={{res.itemName}}&itemdescription={{res.itemDescription}}"> Click me </a>
    

    And then to work with the encoding, add import urllib and change the line

    name = nam.encode('utf-8')
    

    to

    name = urllib.unquote(nam.encode('utf-8'))