TurboGears RESTful Applications - TurboGears

What is TurboGears RESTful Applications?

Rest stands for REpresentational state transfer. rest is web requirements primarily based structure and uses HTTP Protocol for information communication. It revolves around a useful resource wherein each element is a resource and a aid is accessed by means of a common interface the usage of HTTP widespread techniques. Rest changed into first introduced by Roy Fielding in 2000.

What is a Rest Controller

RestController in TurboGears provides a mechanism to access the request’s technique, not just the URL. standard HTTP verbiage includes: GET, post, put, and DELETE. The RestController supports these, and also provides some shortcuts for URL dispatch that makes displaying the facts as forms and lists, a touch simpler for the consumer.

To explain how RESTful works with TurboGears, we are going to define a simple webservice that exposes a list of college students.

The code for student model is given below –

Now create a controller based on RestController and provide a view function to list out list of students in json format.


Mount this StudentController in RootController of application by incorporating following lines in root.py −

Going to the http://localhost:8080/students it will provide the list of our students encoded in json format.

We use the post method to define how we go about saving our student to the database. This method gets called whenever the http://localhost:8080/student url is accessed using a POST request −

Using the get_one() method, we can display one item from the database to the user −

PUT is the method used for updating an existing record using REST −

The work-horse of delete is attached to the post_delete method. Here we actually remove the record from the database, and then redirect back to the listing page −

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

TurboGears Topics