Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

450
Visualizações
How to print something when I use django REST framework?

I use django REST framework. In my views.py has a class:

class TaskViewSet(viewsets.ModelViewSet):

    queryset = Task.objects.all().order_by('-date_created')
    serializer_class = TaskSerializer
    print("leo test in TaskViewSet")

I want to print("leo test in TaskViewSet") when I call the url every time.

But it will only print the 1 time after I use runserver cmd.

Anyone know how to print("leo test in TaskViewSet") every time when I call the api url.

My urls.py:

from django.conf.urls import url,include
from django.contrib import admin
from rest_framework import routers
from trips.views import TaskViewSet


router = routers.DefaultRouter()
router.register(r'task',TaskViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #url(r'^$', hello_world),
    url(r'^', include(router.urls)),
]

Thank you very much.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have to define actions to get the desired behavior. By default, the router you have defined bound to several actions like create, retrieve, list, update and destroy. So when you implement those actions via methods as below you would be able to print on every endpoint call.

class TaskViewSet(viewsets.ModelViewSet):

    queryset = Task.objects.all().order_by('-date_created')
    serializer_class = TaskSerializer
    print("leo test in TaskViewSet")

    def list(self, request):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)

    def create(self, request):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def retrieve(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def update(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def partial_update(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def destroy(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)

Let me know if something is not clear. Also please read here for more details http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda