Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically call a Django Rest Framework view within another view?

I have the following generic class based views built with Django Rest framework (DRF)

class ExampleDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Example.objects.all()
    serializer_class = ExampleSerializer
    renderer_classes = (JSONRenderer, TemplateHTMLRenderer)

    def get(self, request, *args, **kwargs):

        response = self.retrieve(request, *args, **kwargs)
        if request.accepted_renderer.format == 'html':
            form = ExampleForm(data=response.data)
            return Response({'data': response.data, 'form': form}, template_name='example.html')

        return response

This view allow me to obtain both JSON data or HTML form from the same endpoint by specifying the format=json or html.

I would like to programmatically call that view to obtain the rendered HTML form from within another view in order to include this form in another page that will include more stuff.

like image 863
Etienne Desgagné Avatar asked Nov 06 '14 22:11

Etienne Desgagné


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.

What is renderers in Django REST Framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.

Is Django REST Framework synchronous?

Django REST framework is built on Django, which is a synchronous framework for web applications. If you're already using a synchronous framework like Django, having a synchronous API is less of an issue.


3 Answers

I found the solution for this in the documentation... https://docs.djangoproject.com/en/1.7/topics/class-based-views/mixins/

Hint is from their example here:

class AuthorDetail(View):

    def get(self, request, *args, **kwargs):
        view = AuthorDisplay.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = AuthorInterest.as_view()
        return view(request, *args, **kwargs)
like image 65
user Avatar answered Oct 19 '22 12:10

user


html_from_view = ExampleDetail.as_view({'get': 'list'})(request).content

OR

html_from_view = ExampleDetail.as_view({'get': 'retrieve'})(request, pk=my_id).render().content
like image 36
Etienne Desgagné Avatar answered Oct 19 '22 11:10

Etienne Desgagné


As of Django 2.2 and DRF 3.9.2 I am able to get response using below code.

response = UserItemsApiView.as_view()(request=request._request).data

Above example solves below issues:

  • The request argument must be an instance of django.http.HttpRequest, not rest_framework.request.Request
  • Instead of content, using data attribute gave me result from that view.
like image 17
JD Solanki Avatar answered Oct 19 '22 12:10

JD Solanki