Setting Up RESTful APIs with Flask or Django

 Setting up RESTful APIs with Flask or Django is a common task for building web backends in Python. Here’s a high-level guide to help you decide which one to use and how to get started with each.


✅ When to Use Flask vs Django

Feature Flask Django

Philosophy Microframework (minimalist) Full-stack framework

Flexibility High – choose your own tools Convention over configuration

Learning Curve Easier for small projects Steeper due to built-in components

Best For Small to medium APIs, microservices Large applications, built-in admin


🔧 Flask RESTful API Setup

1. Install Flask

bash

Copy

Edit

pip install Flask

2. Basic API Example

python

Copy

Edit

from flask import Flask, jsonify, request


app = Flask(__name__)


@app.route('/api/hello', methods=['GET'])

def hello():

    return jsonify({"message": "Hello, world!"})


@app.route('/api/echo', methods=['POST'])

def echo():

    data = request.json

    return jsonify({"you_sent": data})


if __name__ == '__main__':

    app.run(debug=True)

3. For More Structure

Use Flask-RESTful:


bash

Copy

Edit

pip install Flask-RESTful

python

Copy

Edit

from flask import Flask

from flask_restful import Resource, Api


app = Flask(__name__)

api = Api(app)


class HelloWorld(Resource):

    def get(self):

        return {"message": "Hello, RESTful World"}


api.add_resource(HelloWorld, '/api/hello')


if __name__ == '__main__':

    app.run(debug=True)

🧰 Django RESTful API Setup (with Django REST Framework)

1. Install Django and DRF

bash

Copy

Edit

pip install django djangorestframework

2. Create Project and App

bash

Copy

Edit

django-admin startproject myproject

cd myproject

python manage.py startapp api

3. Configure settings.py

Add to INSTALLED_APPS:


python

Copy

Edit

'rest_framework',

'api',

4. Create a Serializer and View

python

Copy

Edit

# api/serializers.py

from rest_framework import serializers


class HelloSerializer(serializers.Serializer):

    message = serializers.CharField(max_length=100)

python

Copy

Edit

# api/views.py

from rest_framework.views import APIView

from rest_framework.response import Response


class HelloApiView(APIView):

    def get(self, request):

        return Response({'message': 'Hello from DRF!'})

5. Set Up URLs

python

Copy

Edit

# api/urls.py

from django.urls import path

from .views import HelloApiView


urlpatterns = [

    path('hello/', HelloApiView.as_view()),

]


# myproject/urls.py

from django.contrib import admin

from django.urls import path, include


urlpatterns = [

    path('admin/', admin.site.urls),

    path('api/', include('api.urls')),

]

6. Run the Server

bash

Copy

Edit

python manage.py runserver

🧪 Testing Your API

Use tools like:


Postman


curl


JavaScript fetch() or Python requests


🚀 Final Thoughts

Use Flask if you need something lightweight and flexible.


Use Django + DRF if you want batteries-included features (auth, ORM, admin) with less manual setup.

Learn Full Stack Python Course in Hyderabad

Read More

Flask vs Django for Full Stack Development: A Comparison

How to Use Django Models for Database Management

Introduction to Object-Relational Mapping (ORM) in Python

Python Database Management: PostgreSQL vs MySQL

Visit Our Quality Thought Training in Hyderabad

Get Directions



Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Why Data Science Course?

How To Do Medical Coding Course?