Django or Django Rest Framework

后端 未结 7 767
北荒
北荒 2021-01-30 06:28

I have made a certain app in Django, and I know that Django Rest Framework is used for building APIs. However, when I started to read about Django Rest Framework on their websit

相关标签:
7条回答
  • 2021-01-30 06:49

    In simple words,

    Django : It provide features for standard web app

    Django Rest Framework: It provide features to build standard REST API.

    0 讨论(0)
  • 2021-01-30 06:52

    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.

    You can build a restful api using regular Django, but it will be very tidious. DRF makes everything easy. For comparison, here is simple GET-view using just regular Django, and one using Django Rest Framework:

    Regular:

    from django.core.serializers import serialize
    from django.http import HttpResponse
    
    
    class SerializedListView(View):
        def get(self, request, *args, **kwargs):
            qs = MyObj.objects.all()
            json_data = serialize("json", qs, fields=('my_field', 'my_other_field'))
            return HttpResponse(json_data, content_type='application/json')
    

    And with DRF this becomes:

    from rest_framework import generics
    
    
    class MyObjListCreateAPIView(generics.ListCreateAPIView):
        permission_classes = [permissions.IsAuthenticatedOrReadOnly]
        serializer_class = MyObjSerializer
    

    Note that with DRF you easily have list and create views as well as authentication.

    0 讨论(0)
  • 2021-01-30 06:54

    Here is a link which might give you some insights on what you are looking for. This explains how a django app can be converted into Django Rest Service, and it also explains what a REST Service actually is

    https://opensourceforu.com/2017/11/converting-django-app-rest-api/

    0 讨论(0)
  • 2021-01-30 07:01

    the old school used Jinja2 with html5, css3, but this became inconvenient when Single Page Applications became popular.API helps to provide information more convenient for the frontend programmer. You no longer work with the frontend. The API is convenient when developing as Web applications and when developing applications for the phone.

    0 讨论(0)
  • 2021-01-30 07:06

    Whether you need an api depends on what you want to do. For example, if you want to access everything in your Django models from a mobile device AND a web-app, you would want to use DRF.

    Why? Consider the case that you are developing an iOS app that users can sign into and you want to use Django as your backend. If you also want to have a website that they can use to change around their app's profile information (say, update their email address or upload a picture), you would need a way to share the information in your Django models with both a website and the iOS device. How can this be done? By giving the user the ability to create/read/update/delete data by simply telling it a url to go to. Now, if you want to access information from the model you can do it from multiple devices, since any device can visit a url.

    However, if you're just building a simple webapp/webpage and keep it all in one place, you can just use straight Django.

    Side note: it's a pretty popular opinion that you should try to separate your frontend from your backend as much as possible. In this case, if you want to use some frontend development framework like React, Angular, or Vue, it's gonna get messy trying to include all those resources in the Django template pages even if you only want a simple web app/web page. In this case, you would use DRF to set up your backend, and just hit the urls from the frontend using a tool like axios. In this scenario, your frontend would likely be hosted on something like Node.

    Again, what you decide to use really just depends on what you want out of your app/site and how comfortable you are with each tool.

    0 讨论(0)
  • 2021-01-30 07:09

    The basic purpose of DRF is to provide APIs. APIs are the touch point in an app. Imagine a project where you have a Django wizard and someone on your team is a JavaScript expert and you want to develop mobile app. The JavaScript expert talks about web elements and threading and the Django expert talks templates and ORM, now what? API is your answer. Let Django wiz give APIs and JSON and let JavaScript wiz do his front end magic. Simplest answer I can think of.

    0 讨论(0)
提交回复
热议问题