Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Sometimes while using web2py, one might wonder where web2py keeps the original filename when downloaded, what happens upon deleting a record or is it possible to ensure that the file is deleted if the record is deleted, how to deal with situations where reference fields link tables and how to pre-populate a form.
To find answers to such questions, let us learn about storing the original filename, autodelete, links to referencing records and pre-populating the form in web2py.
Storing the original filename
When a file is downloaded, web2py automatically keeps the original filename inside the new UUID filename and retrieves it. The original filename is saved in the content-disposition header of the HTTP response after being downloaded. Everything is done transparently; there is no need for programming.
On rare occasions, it could be useful to keep the original filename in a database field. In this situation, you must alter the model and provide a field to store it in:
Upon deleting a record, the SQLFORM does not delete the physical uploaded file(s) that the record references. This is because web2py does not know if the same file is used/linked by other tables or used for a different purpose. You can do the following when the corresponding record is deleted and you are certain if it is safe to delete the actual file:
The owner of dog is required to reference a valid db.p_person.id by '%(n_name)s'.
Let's add some people and their dogs using the appadmin interface for this application.
The appadmin UPDATE form displays a link to a page listing the dogs that belong to that person when editing an existing person. The SQLFORM's linkto argument can be used to simulate this behaviour. The URL of a new action that gets a query string from the SQLFORM and lists the related records must be pointed to by linkto.
For Example -
def display_form():
record = db.p_person(request.args(0)) or redirect(URL('index'))
link = URL('list_records', args='db')
form = SQLFORM(db.p_person, record, deletable=True, linkto=link)
if form.process().accepted:
response.flash = 'form -> accepted'
elif form.errors:
response.flash = 'form has -> errors'
return dict(form=form)
You can also try this code with Online Python Compiler
with the accompanying "default/list_records.html" view:
{{extend 'layout.html'}}
{{=records}}
Pre-populating the form
Pre-populating a form is always possible using the following syntax:
form.vars.name = 'field_value'
Regardless of whether the field (in this case, "name") is explicitly visualised in the form, statements like the one above must be added after the form declaration and before the form is accepted.
Frequently Asked Questions
What is web2py?
Web2py is a web application framework which is free and open-source, written in the Python programming language.
Why web2py?
Users can learn easily server-side web development, and it is lightweight and speedy.
What is Database Abstraction Layer?
A database abstraction layer is an API (Application Programming Interface) which unifies the communication between a computer application and databases such as Oracle, SQL Server, MySQL, IBM Db2 and PostgreSQL.
What is API?
API (Application Programming Interface) is a means of communication between two or more computer programs. It is a kind of software interface that provides a service to other software programs.
What is meant by Pre populating?
Pre populating means entering/filling in data in advance or, technically, populating form fields or databases in advance.
Conclusion
In this article, we discussed storing the original filename, autodelete attribute, links to referencing records and pre populating the form in web2py. We learnt where web2py keeps the original filename when downloaded, what happens upon deleting a record, what syntax to use to ensure that the file is deleted if the record is deleted, how to deal with situations where reference fields link tables and how to pre-populate a form.