pythonurlflaskwtforms

How to render (make clickable) a url in a stringfield using Wtforms


I am trying to make clickable a URL in a Wtforms Stringfield.

So far the url is populated into the Stringfield but it is not clickable/clicking doesn't rediret to the webpage because it is text.

See picture of the form:

enter image description here

My js code is:

function update_Company_Name()
{
    var my_isin_id = $('#isin_id').val();
    $.getJSON( $SCRIPT_ROOT + '/ajax_update_event_table',
            {
                isin_id: my_isin_id

            }, function(data)
            {
                if (data.success!=false){
                    $('#url_main').val(data.success['url_main']);
                }
                else{
                    alert('failed to update url box');
                }

            }
        );

};

the python/flask code behind is:

from wtforms import StringField as _StringField

............blahbla
url_main = _StringField(label='Main Website')

My current workaround having taken onboard information given in comments.

In my flask/Wftforms code is added a render:

url_main = _StringField(label='Main Website', render_kw={'onclick':'open_WebPage(this.value)', 'type':'button', 'readonly':None})

Which shows the form as a clickable, I guess next step is use css to show it as a hyperlink

and i added this java script function on the Html page:

<script>

    function open_WebPage(url_to_open)
    {
        if(url_to_open)
        {
            window.open(url_to_open);
        }
    };
</script>


It does the trick, although I was hoping a simpler solution provided by maybe another Wftforms component or else/I am new to js, html python.


Solution

  • This should work

    from Flask import Markup
    
    url_label = Markup("<a href='YOUR_URL'>Main Website</a>")
    url_main = _StringField(label=url_label)