How to decode a url string in a flask template


Question

I am using flask.

In my template I used this to encode a string.

encodeURIComponent(mytag)

Now I want to decode in another template.
Where is what the string looks like.

%26lt%3B!--Afff%20Tracking%20Tag--%26gt%3B%0A%26lt%3Bimg%20src%3D%22http%3A%2F%2Fcm.g.doubleclick.net%2Fpixel%3F%0Agggg_nid%3Dhff%26gggg_cm%26avid%3David3966173574%26campaign_id%3Dundefined%22%20%2F%26gt%3B

In the template how do I decode the string?

{% for crid, object in tag_handler.iteritems() %}
    <script>var x = decodeURI("{{object['tag_display']}}"); alert(x);</script>
        <div id="tagBox" style="display: block;width: 700px">
            <pre class="prettyprint">
                <code class="language-html">    
                    {{object['tag_display']}}
                </code>
            </pre>
       </div>

{% endfor %}

I am using google pretty to display the string.

1
3
9/5/2013 7:24:25 PM

You can use Python's urllib. There are a couple of options, unquote and unquote_plus. See https://unspecified.wordpress.com/2008/05/24/uri-encoding/ for explanation. In brief, "Choose unquote_plus if your URLs use ‘+’ for spaces, and remember that HTML forms do this automatically."

from urllib import unquote_plus
@app.route('/foo', methods=['POST'])
def foo():
    mytag = request.form['params']
    print unquote_plus(mytag)
1
2/23/2016 12:12:33 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon