Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11523

Django REST Framework not generating form in Browsable API

Ok, I have a GenericAPIView that is supposed to generate a form in the Browsable API cause it declares a post method:

from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from transacciones.serializers import BillSerializer


class ProcessBill(GenericAPIView):

    serializer_class = BillSerializer

    def post(self, request):

        recieved_data = request.data

        print(recieved_data)

        return Response("Processed Bill", status=status.HTTP_200_OK)

But it doesn't generate a form. The view looks like this (it is in spanish, I translated the code to english so it is be more confortable to read):

enter image description here

I understand I get a 405 Method not Allowed cause I don't define a get method, only a post.

Im using Django 1.8, Django REST Framework 3.3.1 and python 3.4. I'm lost here. Any ideas?

EDIT

Also, if I use Postman (Chrome add on) the view responds correctly.

Upvotes: 2

Views: 4222

Answers (4)

Arjen
Arjen

Reputation: 646

I ran into the same problem. In my case, the mistake was in the serializer, where I used django.db.models.CharField(...) instead of rest_framework.serializers.CharField(...).

Upvotes: 0

Natiq Vahabov
Natiq Vahabov

Reputation: 504

I had the same issue, with postman I saw it was a credential issue, so changing current user status to staff solved the problem.

Upvotes: 0

SuperNova
SuperNova

Reputation: 27466

If you are using generics.RetrieveUpdateDestroyAPIView then you will have to define permission_classes which can be custom.

class ObjectDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    Object RUD (Read Update Delete) API
    """
    permission_classes = ( checkUserPermission, )
    queryset = Object.objects.all()
    serializer_class = ObjectSerializer

OR If you want to just test this, use permission_classes = ( AllowAny, )

class ObjectDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    Object RUD (Read Update Delete) API
    """
    permission_classes = ( AllowAny, )
    queryset = Object.objects.all()
    serializer_class = ObjectSerializer

Upvotes: 2

Geotob
Geotob

Reputation: 2945

You can use this

from rest_framework.generics import CreateAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.response import Response

from transacciones.serializers import BillSerializer


class ProcessBill(CreateModelMixin, GenericAPIView):
    serializer_class = BillSerializer

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        # Do whatever you want here

        # Then invoke the create method and create your instance
        return super().create(request, *args, **kwargs)

Or as an alternative, you just inherit from CreateAPIView which does essentially the same as the code above :)

Upvotes: 1

Related Questions