analitics

Pages

Friday, April 26, 2019

Python 3.7.3 and Django REST framework.

Today I tested something simpler for beginners: Django REST framework.
Once you understand how it works then it's simple to use.
This tutorial does not address the security issues generated by the REST, Django framework.
The official webpage comes with many information and technical specifications for this API:
Django REST framework is a powerful and flexible toolkit for building Web APIs.
The example I've submitted is built into the Scripts folder because I did not use the virtual environment.
Let's start installing the python module.
C:\Python373\> cd Scripts
C:\Python373\Scripts\>pip install djangorestframework
C:\Python373\Scripts\>django-admin startproject example
C:\Python373\Scripts\>cd example 
C:\Python373\Scripts\>python manage.py migrate 
C:\Python373\Scripts\>python manage.py createsuperuser
C:\Python373\Scripts\>python manage.py startapp test001
In the example folder we will make changes.
First is the settings.py file:
INSTALLED_APPS = [
...
    'rest_framework',
    'test001']
The next file is the urls.py:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('test001.urls'))
]
Also make changes in the test001 folder
You must create a python script and call it urls.py:
from django.urls import path, include

# this urlpatterns will fill later 
urlpatterns = []
You make changes to the file models.py and create a named class test.
This class will have two fields that we will update.
from django.db import models

class test(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
44/5000
The following commands will synchronize the database:
C:\Python373\Scripts\example>python manage.py makemigrations
...
  test001\migrations\0001_initial.py
    - Create model test
C:\Python373\Scripts\example>python manage.py migrate
...
Running migrations:
...
Another step is to add the test into admin.py script:
from django.contrib import admin
from .models import test
admin.site.register(test)
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON , XML or other content types.
Let's create an serializers.py python script into test001 folder and use this code:
from rest_framework import serializers
from .models import test

class test_serializer(serializers.ModelSerializer):
    class Meta:
        model = test
        fields = ('id', 'first_name', 'last_name')
Also is need to update the views.py python script:
from django.shortcuts import render
from rest_framework import viewsets
from .models import test
from .serializers import test_serializer

class test_view(viewsets.ModelViewSet):
    #query to get all information from database
    queryset = test.objects.all()
    serializer_class = test_serializer
Now because is all create the last step is to fix the urls.py from test001 folder with the routers.
The REST framework adds support for automatic URL routing to Django.
from django.urls import path, include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('test001', views.test_view)
#add to path 
urlpatterns = [
    path('', include(router.urls))
]
Now you can test it with:
C:\Python373\Scripts\example>python manage.py runserver
If you want to make changes into models.py then you will need to use the commands to synchronize the database after these use"commands
makemigrations and migrate to fix errors.
If you encounter such run-time errors
"GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667..."
These errors can be the result of settings like DEBUG, STATICFILES_DIRS or STATIC_ROOT from file settings.py.
Then you need to execute python manage.py collectstatic and Django goes through all directories where static files can be found and places them in your static root.
The result of this tutorial can be see on my youtube channel: