Mr. Yao
Mr. Yao

Reputation: 3

The formview view API in django.views.generic cannot be seen on the swagger page

I am using Django and drf_yasg, and I hope to add this interface to the swagger page when using the form view. However, I have tried several times and cannot achieve it.

The code is as follows:

class CustomRegisterView(FormView):
    template_name = "registration/register.html"
    form_class = RegisterForm
    success_url = reverse_lazy("home")
    swagger_schema = {
        "tags": ["user"],
        "method": "post",
        "operation_id": "register_user",
        "operation_description": "User registration with email and password",
        "request_body": openapi.Schema(
            type=openapi.TYPE_OBJECT,
            properties={
                "first_name": openapi.Schema(type=openapi.TYPE_STRING, default=""),
                "last_name": openapi.Schema(type=openapi.TYPE_STRING, default=""),
                "username": openapi.Schema(type=openapi.TYPE_STRING),
                "email": openapi.Schema(type=openapi.FORMAT_EMAIL),
                "password1": openapi.Schema(type=openapi.FORMAT_PASSWORD),
                "password2": openapi.Schema(type=openapi.FORMAT_PASSWORD),
            },
            required=["username", "email", "password1", "password2"],
        ),
        "responses": {
            status.HTTP_200_OK: openapi.Response(
                description="Successfully registered",
                schema=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={"message": openapi.Schema(type=openapi.TYPE_STRING, description="Successfully registered")},
                )
            ),
            status.HTTP_400_BAD_REQUEST: openapi.Response(
                description="Registration failed",
                schema=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={
                        "detail": openapi.Schema(
                            type=openapi.TYPE_STRING,
                            description="Registration error message",
                        ),
                    },
                ),
            ),
        },
    }

    def post(self, request, *args, **kwargs):
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

It can't help me debug on the swagger page, it is not rendered to the swagger page, what is the main reason? I tried the @swagger_auto_schema decorator, and it didn't work. Is there any other view writing method except drf to the standard API writing method? Can't get the support of drf-yasg? If you want the views in FormView and django.contrib.auth.views to be rendered on the swagger page, how to achieve it?

Upvotes: 0

Views: 247

Answers (1)

Maxim Novoselsky
Maxim Novoselsky

Reputation: 1

I encountered the same problem using drf-spectacular. I'm not sure if the reason is the same for Swagger, but maybe my solution will work for you too.

The reason turned out to be that drf-spectacular discards all views that are not subclasses of the APIView class, including FormView.

I solved it by inheriting both FormView and APIView:

from rest_framework.views import APIView
from django.views.generic import FormView

class CustomRegisterView(FormView, APIView):
    ...

Upvotes: 0

Related Questions