Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework reuse API logic within website

I have just created my first token based web API with Django-Rest-Framework and it has worked really well for my mobile applications.

I am about to start creating a website based on Django, but I would like to reuse as much of my API code as possible.

The options I can see are:

  • Create a basic Django application that consumes my DRF API, would have to add basic auth to my api?
  • Create a third application which contains all my models and logic and then import the code into the API and website application.

How do you normally approach this kind of code reuse with Django?

like image 333
Lunar Avatar asked Sep 03 '14 11:09

Lunar


People also ask

What is the use of REST framework in Django?

Django REST Framework is used to create web APIs very easily and efficiently. This is a wrapper around over the Django Framework. There are three stages before creating an API through REST framework, Converting a Model’s data to JSON/XML format (Serialization), Rendering this data to the view, Creating a URL for mapping to the viewset.

What is the best framework for REST API?

Django REST Framework. Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.

How to get JSON from Django REST framework endpoint?

Click on the “GET” button and select “json” from the dropdown menu. This is what the raw JSON from our API endpoint looks like. I think we can agree the Django REST Framework version is more appealing. We covered a lot of material in this chapter so don’t worry if things feel a little confusing right now.

What is the use of Django?

Django is a Python web framework that provides all the tools necessary for building modern web applications. In this tutorial, we are going to learn how to build a basic API that can be used to serve data to any client-side application including websites, mobile apps, desktop apps, and more.


1 Answers

This answer is based on my personal approach on the problem, both on industry and academic scenarios.

I value software decoupling. As such, I want to build smaller components, as reusable as possible. While presented with a similar problem, I've built the following components:

  • Django Rest Framework with authentication based token and complete REST API for all models;
  • Mobile application that interacts with the server using REST;
  • Web Application (in my case with AngularJS) that interacts with the application using REST.

This approach allows to have different teams working each on their own (backend, mobile, web). I could enumerate tens of advantages of adopting such approach, but I've paved the way and you can consult literature if needed.

Good luck

like image 85
tiagoboldt Avatar answered Oct 21 '22 05:10

tiagoboldt