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 do file validation in web2py and write custom validator to validate files?

In this article, I will explain built in validator IS_IMAGE  and  IS_UPLOAD_FILENAME, also a custom validator.
It is important to validate file uploaded on server to restrict malicious files like .py .php, .exe bat etc.
Web2py provides 2 validators to validate files, IS_IMAGE  and IS_UPLOAD_FILENAME .


It checks whether uploaded file is of type image and file is in any of supported image formats

Following statement Checks if uploaded file is either png or bmp


It checks whether uploaded file meets the given rules for filename and extension.

You can provide regular expressions in filename and extension parameters.
If filename or extension doesn’t match the expression/criteria then it shows error in form.
You can provide your own error message using argument error_message


If you want to allow all files starting with word ‘log’ and having extension “txt”:

Like IS_IMAGE validator we can not pass list of valid extensions to IS_UPLOAD_FILENAME validator.
But we can achieve this using regular expression or by writing our own validator

  1. Using regular expression:

The above expression will allow all the files having extension pdf, docx, doc or xls.

^ Matches the start of string

$ Matches end of the string

(pdf|docx|doc|xls) match any value from group pdf, docx, doc and xls

Read more about Regular Expression

2. Using custom validator:

Following validator checks whether uploaded file has extension from the valid extension list provided through argument extensions. It will also show error for empty file.


Advantage of custom validator is you can add and modify restriction as per your need

If you are facing any issues regarding above post 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.