Answers for "django viewset"

1

django rest framework viewset

from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from myapps.serializers import UserSerializer
from rest_framework import viewsets
from rest_framework.response import Response

class UserViewSet(viewsets.ViewSet):
    """
    A simple ViewSet for listing or retrieving users.
    """
    def list(self, request):
        queryset = User.objects.all()
        serializer = UserSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None):
        queryset = User.objects.all()
        user = get_object_or_404(queryset, pk=pk)
        serializer = UserSerializer(user)
        return Response(serializer.data)
Posted by: Guest on August-19-2021
2

create views django

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hola(request):
    return HttpResponse("Hola Mundo, Esto es Django")
Posted by: Guest on May-28-2021
0

django class based views

# some_app/views.py
from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = "about.html"
Posted by: Guest on August-23-2021
0

get_permissions

def get_permissions(self):
    # Your logic should be all here
    if self.request.method == 'GET':
        self.permission_classes = [DummyPermission, ]
    else:
        self.permission_classes = [IsAuthenticated, ]

    return super(UsersViewSet, self).get_permissions()
Posted by: Guest on October-08-2020

Code answers related to "django viewset"

Python Answers by Framework

Browse Popular Code Answers by Language