0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Je Je
  • 508
  • 2
  • 8
  • 23
  • 1
    This is more of a general HTML/JavaScript type question.. see https://stackoverflow.com/questions/3060055/link-in-input-text-field – SuperShoot Oct 24 '19 at 11:41
  • Thanks for implicitly pointing out that you can't have an hyperlink clickable with input/Stringfield. I am new to this. I have updated my question with current answer/workaround in case that helps anyone else. – Je Je Oct 24 '19 at 19:08

1 Answers1

2

This should work

from Flask import Markup

url_label = Markup("<a href='YOUR_URL'>Main Website</a>")
url_main = _StringField(label=url_label)
Rakesh
  • 158
  • 1
  • 9