Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Django-Rest-Framework (DRF) Views and Serializers replace Django native views and forms

I am developing a website where i have to show

1) web pages with html content-type

2) also provide api json end points to develop web or mobile apps.

Presently for html web pages i have used djangos views and forms.

And for api i was using Django rest frameworks views and serializers.

But after going through DRF i have found that DRF can render multiple formats.

HTML & Forms REST framework is suitable for returning both API style responses, and regular HTML pages. Additionally, serializers can used as HTML forms and rendered in templates.

http://www.django-rest-framework.org/topics/html-and-forms/

You can use TemplateHTMLRenderer either to return regular HTML pages using REST framework, or to return both HTML and API responses from a single endpoint.

http://www.django-rest-framework.org/api-guide/renderers/#templatehtmlrenderer

Since with one view (one endpoint) i can get both the html and api will that not make my coding efforts less.

I am planning to use only DRF views and searializers instead of DJango forms and views for any type of content.

What will be the setbacks if I only use DRF.

like image 506
Santhosh Avatar asked Apr 24 '18 13:04

Santhosh


1 Answers

One cannot use DRF without Django since DRF is not an autonomous framework.
So in order to use DRF, you will base it on a Django app. Therefore the option to use Django views or not is up to you and the problem you want to solve.

Usually, there are no drawbacks on using only the DRF views and serializers, but as I mentioned, that depends on the problem at hand. Some threads listing some reasons on why to use DRF can be found here and here.

Finally, DRF gives you the option to use a "situational" renderer with the option request.accepted_renderer (example straight from the documentation):

In some cases you might want your view to use different serialization styles depending on the accepted media type. If you need to do this you can access request.accepted_renderer to determine the negotiated renderer that will be used for the response.

For example:

@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def list_users(request):
    """
    A view that can return JSON or HTML representations
    of the users in the system.
    """
    queryset = Users.objects.filter(active=True)

    if request.accepted_renderer.format == 'html':
        # TemplateHTMLRenderer takes a context dict,
        # and additionally requires a 'template_name'.
        # It does not require serialization.
        data = {'users': queryset}
        return Response(data, template_name='list_users.html')

    # JSONRenderer requires serialized data as normal.
    serializer = UserSerializer(instance=queryset)
    data = serializer.data
    return Response(data)    

which covers the second part of your question.

like image 91
John Moutafis Avatar answered Nov 15 '22 00:11

John Moutafis