Simple Django Piston Tutorial
Django Piston is an easy-to-use application that allows you to create your own REST API for your applications. This short tutorial is straight forward and gives you a good starting point to set out your projects.
This project is not only a demonstration of how Django Piston can be integrated but also a continuation of my earlier projects: Django Development With Eclipse and PyDev and Django Comments Framework Tutorial. In the earlier projects, we have created a simple Django application using PyDev and then enhanced it by integrating the Django Comments Framework. Now, what we are going to do is to re-use the application and then improve it by utilising the Django Piston framework to build our REST API.
Download Link: Simple Django Piston Tutorial
As recommended on the official site, you should create a separate folder for your Piston-based REST API as highlighted in the following screenshot with 2 key files handlers.py and urls.py:
In your main urls.py, you should define an entry pointing to the urls.py file in your Piston api folder.
Here from my experience, we should use a custom class for our handlers instead of the BaseHandler class provided by Piston because the custom class CsrfExemptBaseHandler can just remove the csrfmiddlewaretoken key in the request. Hence, you can easily access your API by typing http://localhost:8000/api/piston/YOUR_MODEL. And this method was taken from the site http://andrew.io as shown in the picture:
We have only one model called Article in our project so we can create one handler for it named ArticleHandler. The method read() is used to handle GET requests.
For our Article model, we have declared only 3 attributes: name, value and status. However the status attribute contains integer values so we want to make it more human readable by converting it to the corresponding text value.
class Article(models.Model): STATUS_TYPES = ( (0 , 'Pending'), (1 , 'Reviewed'), (2 , 'Published'), (3 , 'Expired') ) ...........
For further handling, you can read more detailed information on the official site. As can be seen, there are 4 possible methods to be implemented for GET, POST, PUT and DELETE.
Below you can see the relationship between the declaration in urls.py and handlers.py:
Following is the end result you can get from this:
1)Excel How To Merge Cells Into Single Cell Separated By Comma
2)Why Python Doesn't Import All Libraries Like In Java
3)How To Use Relative Paths In Django Settings
4)Django Comments Framework Tutorial
5)PyDev Unresolved Import Error