Django REST API project for learning purposes. It has been developed following this tutorial. Below I explain some of the steps followed, as a way to internalize what has been learned and as notes that will be useful in case I want to carry out a new similar project.
Is assumed Python has been installed previously and the used IDE is Visual Studio Code (VSC).
Enter the following commands on a console placed in your project folder (using the console offered by VSC directly):
pip install virtualenv
py -m virtualenv venvOne option is to enter the following command:
.\venv\Scripts\activateFor convenience, best option is to select the specific Python interpreter for out virtual environment. We can select it, on VSC, pressing "F1" and searching for "Python select interpreter". The adequate Python interpreter should be higlighted as the recommended one.
Enter the following commands:
pip install django
pip install djangorestframeworkEnter this command:
django-admin startproject [project-name] .Note
Notice the use of ".". If we are working in a folder already, that avoids the need to create an extra folder.
Enter this command:
py manage.py startapp [app-name]Then, go to settings.py inside the folder of our django project and add the name of the created app to the list of INSTALLED_APPS.
Just like in the previous step: go to settings.py inside the folder of our django project and add rest_framework to the list of INSTALLED_APPS.
We can use the following command to run our project in a local server whenever we want:
py manage.py runserverSepecifications about the IP adress and port will be displayed after entering the command.
In the app's folder we can create models, that will be converted to database tables, adding them to the models.py file. Each class will be a table, and each attribute will be a column. id column will be created by default. We can specify the type of data for each attribute using models class methods (eg: models.CharField()).
To migrate this models to a database we must enter the following commands:
py manage.py makemigrations
py manage.py migratemakemigrationscommand scans models changes on the apps registered on the projects INSTALLED_APPS, on settings.py, and create a .py file in the app's migrations folder, describing the changes to be made to the database.
migratecommand applies the specified changes: reads pending migrations files and executes them orderly to synchonize the models with the database, modifying the actual structure of the database. It also updates the Django's migrations register, to make sure this is applied just once.
In the app's folder we create a serializers.py file. There, we import Django REST's serializers and our before created model and create a serializer:
Django REST serializers transform complex data (like instances of Django models) to smple Python types that can be then easily rendered to formats like JSON or XML, that are more suitable to be send as a response when using an API. They also perform the opposite operation: transforming simple data to complex data. Moreover, serializers will allow us to call an special model of REST framework (ModelSerializer).
Take a look at docs for more info on Django's REST framework serializer.
Create api.py file in the app's folder. First, import our Project model and our ProjectSerializer serializer and viewsets and permissions from Django REST. Then, create a ViewSet (ModelViewSet), which has CRUD functionalities, and where we can specify data to be consulted, permissions for viewing the data and the serializer used to transform the data:
Create urls.py file in the app's folder. Import routers module from Django REST and our before created ModelViewSet, create a router, generate the CRUD routes and add them to the files URLs in urlpatterns:
Then, in project's folder urls.py file import include module and add the app's routes by using include() into the urlpatterns of this file.
If we enter the py manage.py runserver command, now we can access to the "api/projects/" route of the project, where we can view, write, update and delete data.
If we want to access to a project's specific data, we just need to enter the id into the URL:
We can also access the API and interact with it using a REST Client. For this purpose we can install a VSC extension named Thunder Client. The next image shows the client interacting with the API:
Warning
These steps are better followed directly from the tutorial, what I encourage to do, as is needed to create accounts on external services and use these services, which I won't cover, etc. Information given below covers just general aspects and some troubleshooting.
Create a render.com account and use free trial Create a git repository and edit .gitignore (I didn't edit .gitignore as recommended, which may had affected to the database data showing when I deployed the app)
The tutorial follows some of the Deploy Django Render's article. From here we should pay attention to the following sections: Adding basic security, Adding PostgreSQL support, Set up static file serving, Create a build script.
Some of the commands needed while following the Render's article are:
pip install whitenoise[brotli] for installing whitenoise, as Render cannot serve static files
pip install gunicorn for installing gunicorn, a Web Server Gateway Interface (WSGI) which Render uses for deployment.
pip freeze > requirements.txt for creating requirements.txt including all dependencies:
While following Adding PostgreSQL support section, I had trouble using the pip install psycopg2-binary command, here is what I did to resolve it:
- Installed PostgrSQL.
- Added the PostgreSQL installation folder route plus "/bin" to the systems
$PATH. - Then, anyways, used the command:
pip install psycopg2-binary --global-option=build_ext --global-option="-I [folder location for PostgreSQL + /bin](in my case, it showed another error related with C++). - Installed Microsoft C++ Build Tools, selecting the option labelled with "Desktop development with C++".
I also had trouble when trying to change build.sh permissions, as I was using Windows and changing permissions don't work the same as Linux there. Using the chmod a+x build.sh command seemed useless, as ls -l build.sh showed no execution permissions for the file. Anyways, I tried following along the tutorial and everything worked fine.







