how to redirect to an 'external' url in a view?

362 views
Skip to first unread message

Steven Vannoy

unread,
Aug 28, 2016, 5:29:40 PM8/28/16
to web2py-users
I have a view that displays a database form. One of the fields in that form is my user's own website. I'd like to display that as an active link in the browser. I've tried two different forms, the first produces an error because it is trying to resolve to a view within my application, and the other just reloads the current page.

Here is the code in my view

{{if site.website is not None:}}
{{=A(T(
"Web Site"), _href=site.website)}}
<br>
<a href={{site.website}}>{{=site.website}}</a>
{{pass}}

Any help is much appreciated 


luis.va...@metamaxzone.com

unread,
Aug 28, 2016, 6:57:37 PM8/28/16
to web2py-users
Hello!

Try this:

<a href="{{=site.website}}" target="_blank">{{=site.website}}</a>

Steven Vannoy

unread,
Aug 28, 2016, 7:25:10 PM8/28/16
to web2py-users
That seemed promising but gave me an exception with top-most error as:

<type 'exceptions.ValueError'> invalid literal for long() with base 10: 'www.huffingtonpost.com'


You can see that I'm using 'www.huffingtonpost.com' as my test case, which does resolve correctly if pasted strait into the browser.

luis.va...@metamaxzone.com

unread,
Aug 28, 2016, 7:31:17 PM8/28/16
to web2py-users
Can you post the full traceback? it seems like youre trying to convert the url to some numeric class. So with the full trace back we can get a clear look of that

黄祥

unread,
Aug 28, 2016, 7:38:41 PM8/28/16
to web2py-users
pls try:
{{if site.website:}}
{{=A(site.website, _title = T("Web site"), _target = "_blank", _href = "%s" % site.website) }}
{{pass}}

best regards,
stifan

Steven Vannoy

unread,
Aug 28, 2016, 8:35:02 PM8/28/16
to web...@googlegroups.com
I was trying not to clutter things up. I can tell you though, when I hover over the link with my mouse, the URL that shows up is 127.0.0.1:8000/welcome/view_a_site/www.huffingtonpost.com

My app is 'welcome' and the current view being displayed is 'view_a_site'

This is the same thing when I use the code offered below from the japanese (or chinese) person below, sorry I can't read your name :)

r "welcome"

Ticket ID

127.0.0.1.2016-08-28.20-31-55.64675aeb-bf0a-4eb8-b97c-686363adb926

<type 'exceptions.ValueError'> invalid literal for long() with base 10: 'www.huffingtonpost.com'

Version

web2py™

Version 2.14.6-stable+timestamp.2016.05.10.00.21.47

Traceback

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

Traceback (most recent call last):

  File "/Users/zabgay/Documents/GitHub/maprac/web2py/gluon/restricted.py", line 227, in restricted

    exec ccode in environment

  File "/Users/zabgay/Documents/GitHub/maprac/web2py/applications/welcome/controllers/default.py", line 173, in <module>

  File "/Users/zabgay/Documents/GitHub/maprac/web2py/gluon/globals.py", line 417, in <lambda>

    self._caller = lambda f: f()

  File "/Users/zabgay/Documents/GitHub/maprac/web2py/applications/welcome/controllers/default.py", line 162, in view_a_site

    site = db(db.sites.id == request.args(0)).select().first()

  File "/Library/Python/2.7/site-packages/pydal/objects.py", line 2020, in select

    return adapter.select(self.query, fields, attributes)

  File "/Library/Python/2.7/site-packages/pydal/adapters/sqlite.py", line 123, in select

    return super(SQLiteAdapter, self).select(query, fields, attributes)

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 1283, in select

    sql = self._select(query, fields, attributes)

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 1170, in _select

    sql_w = ' WHERE ' + self.expand(query) if query else ''

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 952, in expand

    rv = op(first, second, **optional_args)

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 846, in EQ

    self.expand(second, first.type))

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 962, in expand

    rv = self.represent(expression, field_type)

  File "/Library/Python/2.7/site-packages/pydal/adapters/base.py", line 1435, in represent

    return str(long(obj))

ValueError: invalid literal for long() with base 10: 'www.huffingtonpost.com'


Thank you for helping me work on this

S

Steven Vannoy

unread,
Aug 28, 2016, 8:37:33 PM8/28/16
to web2py-users
Hi, See below (or above) where I provide the entire traceback. Your code does the same thing as Luise's in that when I hover over the link, it shows it will try to resolve to: 127.0.0.1:8000/welcome/view_a_site/www.huffingtonpost.com

My app is 'welcome' and 'view_a_site' is the current view which is displaying the link

Anthony

unread,
Aug 28, 2016, 9:54:37 PM8/28/16
to web2py-users
In HTML, if you want to specify an absolute URL, the "href" attribute must start with "http(s)://". Otherwise, the "href" attribute is considered to be relative to the URL of the current page (i.e., the browser simply appends it to the current URL). The URL you see when hovering over the link is generated by the browser, not web2py (if you look at the page's source code, you will simply see href="www.huffingtonpost.com", which the browser assumes is really a request for http://127.0.0.1:8000/welcome/view_a_site/www.huffingtonpost.com).

You are receiving the traceback because the view_a_site action expects an integer in request.args(0) (which is used in a DAL query), but of course it is instead the string "www.huffingtonpost.com".

So, just change the href to "http://www.huffingtonpost.com".

Anthony

Anthony

unread,
Aug 28, 2016, 9:56:30 PM8/28/16
to web2py-users
{{=A(site.website, _title = T("Web site"), _target = "_blank", _href = "%s" % site.website) }}

Note, _href="%s" % site.website is simply equivalent to _href=site.website (no reason for the string formatting). The problem here is with the actual value of site.website, not how it is specified as the href value.

Anthony
Reply all
Reply to author
Forward
0 new messages