Answer the question
In order to leave comments, you need to log in
What is the correct way to write methods in CBV in Django?
I work with the API and until now I have been writing logic in the view creating ordinary functions. Each time I had to repeat 2-3 lines of code at the beginning of each function to specify the API_KEY, check if the API is available, etc. In general, following the DRY principle, I began to look for solutions. Is CBV this solution?
So far, everything seems much more complicated than writing FBV-style logic.
The main question is: how to create a parent class, describe my own methods in it (each receives different data via API), and so that the principle of operation is the same as in FBV: accept a request (request), read its values (in get , post), process them ( I don't use a database ), and pass the context to the template.
I'm not strong in OOP yet and I'm trying to figure it out.
Help me to understand.
Answer the question
In order to leave comments, you need to log in
I don't think you have to do everything from scratch. You need to use the Django REST Framework, there are already base classes and there is already the logic that you described. In particular, there is also authorization by API token. You just need to read the documentation, understand how everything works there, and make your own classes by inheriting APIView (or something else that suits you better).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question