How to assign python variable to Javascript variable in web2py

You might be using following statement to assign python variable to javascript variable on older versions of web2py (<2.9.11), may be because of this question on stackoverflow:

Above statement was working on web2py 2.9.11 and before because of behavioral bug in web2py, which got fixed in web2py 2.9.11. Now response.json()  sets content-type = application/json . So because of above line, on web2py >2.9.11 html code is displayed in browser instead of rendering it as html.

Correct way:

First convert python variable to json in controller and then pass it to view. And in view, use XML() to assign it to javascript variable so that it is not escaped



Other solution is use ASSIGNJS html helper




How to change admin app url in web2py?

This question was asked here How to rename “/admin” to other URL in web2py?

For all the web2py applications, web2py admin url is same, i. e. or One should prevent access to admin interface  to public or external users. Admin url should be changed from /admin to /some-other-url which is difficult to guess. admin is just another app like welcome or example app, so changing appliacation name will change url to it.

For example to change admin  to w2p-adm2in  do following steps

1. Rename admin folder with w2p-adm2in
On linux

On Windows

If you are in local environment, you can also do it using your file explorer

2. Fix broken links
Due to changes done in Step 1, links to admin app are broken. Links to admin app needs to be replaced with links to w2p-adm2in.

In app_name/controllers/
Update, change it to following

3. Instead of admin app, check user is logged in to w2p-adm2in app
while accessing appadmin, by default controller app_name/controllers/ checks whether user is logged in to admin app or not, if not then it redirects to admin app(admin app login page), now it should check whether user is logged in to w2p-adm2in app and if not it should redirect to w2p-adm2in. To acheive this, pass other_application='w2p-adm2in'  as parameter to method gluon.fileutils.check_credentials()

After step 2 and step 3, diff file of app_name/controllers/ will look like following

4. Change links to error page (tickets)
By default links to error is /admin/default/ticket/[ticket_id], Now it should be /w2p-adm2in/default/ticket/[ticket_id]
So add/replace/append following lines to web2py/

If file does not exist in web2py folder then create it containing above lines.


If you more changes that needs to be done, please comment below.

How to write custom download controller in web2py?

Suppose we have employee table with resume as uplaod field. And we want to download file on the basis of employee id.
Download  url is like app_name/custom_download/employee_id
or <a href="{{=URL('default', 'custom_download', args = employee_id)}}" download>


Consider following employee schema:

Then custom download controller will be:

If you have any queries related to this code, please comment below.