Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use both Django and Rest Framework views in a single project?

I want to add some REST API views to an existing Django project, which uses vanilla Django views. For that I'd like to use the REST Framework. I wonder if I can I mix Django and RF views in a single project and what pitfalls this might have (e.g. with authentication).

like image 372
planetp Avatar asked Jun 20 '17 22:06

planetp


People also ask

Can I use django and Django REST framework together?

Django Rest Framework makes it easy to use your Django Server as an REST API. REST stands for "representational state transfer" and API stands for application programming interface. Note that with DRF you easily have list and create views as well as authentication.

Can I use Django REST framework without django?

Ofcourse Django can be done in RESTful way. DRF gives you speed but it needs some learning curve of its own. Without, DRF you need to make decisions in Authentication, Serialization, Versioning AFAIK. Also there will be some work if you parse data in 'PUT' requests.

What is the use of views in Django REST framework?

Django views facilitate processing the HTTP requests and providing HTTP responses. On receiving an HTTP request, Django creates an HttpRequest instance, and it is passed as the first argument to the view function. This instance contains HTTP verbs such as GET, POST, PUT, PATCH, or DELETE.

Is django and django REST same?

Django Rest Framework requires Django as a dependency. Further, Django can do everything that Django Rest Framework can do, you'd just have to write a lot of code to replicate the functionality of Django Rest Framework.


1 Answers

Yes you can surely use both of them at the same time, there shouldn't be any issue. Typically the Django views use SessionAuthentication, adn you will use DRF using TokenAuthentication -- best practice is to add both Session and Token authentication to the authentication_classes in the DRF views - that way you can use the browsable api pages to browse the apis once you have signed in via password (session authentication) as well

class GenericViewTest(SuperuserRequiredMixin, View):
    def get(self, request, *args, **kwargs):

        return HttpResponse("Test")


class PostTrackingCode(CreateAPIView):
    """
    """
    authentication_classes = (SessionAuthentication, TokenAuthentication)  ----> note this
    permission_classes = (permissions.IsAuthenticated,)
    serializer_class = TrackingInfoWriteSerializer
    model = TrackingInfo
like image 129
dowjones123 Avatar answered Oct 12 '22 22:10

dowjones123