Skip to content

Instantly share code, notes, and snippets.

@mredvard
Created April 20, 2024 12:43
Show Gist options
  • Select an option

  • Save mredvard/974e5ab7aa138a24f932f61e35d7d96d to your computer and use it in GitHub Desktop.

Select an option

Save mredvard/974e5ab7aa138a24f932f61e35d7d96d to your computer and use it in GitHub Desktop.
Example of django fastapi integration by llama3 8B

Here is an example of how you can integrate Django and FastAPI:

Let's say we have a project that requires both backend functionality (using Django) and API endpoints (using FastAPI). We'll create a simple blog app with user authentication using Django, and add some API endpoints to retrieve and manipulate blog posts using FastAPI.

Step 1: Create a new Django project

Create a new Django project using django-admin startproject myblog. This will create a basic directory structure for our project.

Step 2: Create the Django app

Inside the myblog directory, create a new app called blog using python manage.py startapp blog. This will create a basic directory structure for our app.

Step 3: Define the models and views in Django

In the blog app, define your models and views using Django's ORM. For example:

# models.py
from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

# views.py
from django.shortcuts import render, redirect
from .models import BlogPost

def blog_post_list(request):
    posts = BlogPost.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

def create_blog_post(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        post = BlogPost(title=title, content=content)
        post.save()
        return redirect('post_list')
    return render(request, 'blog/create_post.html')

Step 4: Create the API endpoints using FastAPI

Create a new file called api.py in the root directory of your project. This will contain our FastAPI app:

from fastapi import FastAPI, HTTPException
from .models import BlogPost

app = FastAPI()

@app.get("/posts/")
async def read_posts():
    posts = BlogPost.objects.all()
    return {"posts": [post.title for post in posts]}

@app.post("/posts/")
async def create_post(title: str, content: str):
    post = BlogPost(title=title, content=content)
    post.save()
    return {"message": "Post created successfully"}

Step 5: Integrate the Django and FastAPI apps

In your wsgi.py file (created by Django), add the following code to integrate the two apps:

import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myblog.settings')

django.setup()

from .api import app as fastapi_app

application = fastapi_app

Step 6: Run the project

Run your project using python manage.py runserver. This will start both the Django and FastAPI apps.

Accessing the API endpoints

You can access the API endpoints by visiting http://localhost:8000/api/posts/ to get a list of blog posts, or http://localhost:8000/api/posts/ to create a new post. You can also use tools like curl or Postman to test the API endpoints.

This is just a basic example, but it demonstrates how you can integrate Django and FastAPI in your project. By using FastAPI for API endpoints and Django for backend functionality, you can take advantage of the strengths of each framework and create a robust and scalable application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment