Skip to content

Latest commit

 

History

History
113 lines (85 loc) · 2.26 KB

django-REST.md

File metadata and controls

113 lines (85 loc) · 2.26 KB

Django REST

Traduzindo REST para PT-BR: Transferência do estado representacional

  1. Instalando
pip install djangorestframework

Se der problema

python -m pip install djangorestframework
  1. (opcional)

No arquivo urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', inicio),
    path('cadastro/', cadastro),
    path('curso/', include('cursos.urls', namespace='cursos'))
    path('api-auth/', include('rest_framework.urls'))
]

No terminal

python .\manage.py startapp rest_api
  1. Registrando no arquivo settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'base',
    'bootstrap5',
    'cursos',
    'rest_framework', 
    'rest_api'   
]

Serializer

Recurso para que outros sistemas possam ver nossa API

  1. Criar um arquivo rest_api/serializers.py
from rest_framework.serializers import ModelSerializer
from cursos.models import Curso


class CursoModelSerializer(ModelSerializer):
    class Meta:
        model = Curso
        fields = '__all__'
  1. No views.py
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from cursos.models import Curso
from rest_api.serializers import CursoModelSerializer
# Create your views here.


@api_view(['GET', 'POST'])
def hello_world(request):
    if request.method == 'POST':
        return Response({'message': f'Hello {request.data.get("nome")}'})
    else:
        return Response({'hello': 'World API'})


class CursoModelViewSet(ModelViewSet):
    queryset = Curso.objects.all()
    serializer_class = CursoModelSerializer
  1. No arquivo urls.pr
from django.urls import path
from rest_api.views import hello_world
from rest_framework.routers import SimpleRouter
from rest_api.views import CursoModelViewSet

app_name = 'rest_api'
router = SimpleRouter(trailing_slash=False)
router.register('curso', CursoModelViewSet)

urlpatterns = [
    path('hello_world/', hello_world, name='hello_world API')
]

urlpatterns += router.urls