Welcome to the Blog API, a simple RESTful API designed to create, update, delete, and fetch blog posts. This project serves as a test of backend architecture, database management, and API development using Django and Django REST Framework (DRF). Follow the instructions below to set up and interact with the API.
- Project Overview
- Setup & Installation
- Database Design
- API Endpoints
- Testing the API
- Running Migrations
- Running the Server
- Technologies Used
- Contributing
This project demonstrates the development of a simple blog API using Django. The API adheres to REST principles, with well-defined routes, proper HTTP methods, and standard error handling mechanisms.
The core features include:
- Creating, updating, and deleting blog posts
- Fetching individual or all blog posts
- Error handling for invalid data and bad requests
- Python 3.x
- Django 4.x (or latest version)
- Django REST Framework (DRF)
- Clone the repository:
git clone https://github.com/jamesunekwuojo/rexversityBlogapi.git
cd api cd Blog- Install the dependencies:
Create a new virtual env (optional)
Then install the dependencies using
requirements.txtfile
pip install -r requirements.txt- Start a new Django project (if not done already):
django-admin startproject Blog
cd Blog- Create the blog app:
python manage.py startapp blogApi- Connect the app to the Django project: In the settings.py file of the Blog project, add blogApi to the INSTALLED_APPS list:
INSTALLED_APPS = [
...
'rest_framework',
'blogApi',
]The blog application uses Django's ORM to handle database operations. Below is the sample model schema for the BlogPost model:
from django.db import models
# Create your models here.
class BlogPost(models.Model): # This gets all of the basic functionality of a database model
title = models.CharField(max_length=255)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True) # Don't need to manually set the time. it's going to automatically fill in the publishhed data.
def __str__(self) :
return self.titleMake sure to run the migrations whenever changes are made to the models.
http://127.0.0.1:8000/api/blogposts/
Endpoint:/api/blogposts/Method:GETDescription:Fetches all blog posts in the database.
GET http://127.0.0.1:8000/api/blogposts/
Endpoint:/api/blogposts/create/Method:POSTDescription:Creates a new blog post.
POST http://127.0.0.1:8000/api/blogposts/create/
Request body:
{
"title": "My First Blog",
"content": "This is my first blog post."
}Endpoint:/api/blogposts/<id>/Method: PUTDescription:Updates the details of an existing blog post by its ID.
PUT http://127.0.0.1:8000/api/blogposts/3/
Endpoint:/api/blogposts/<id>/Method:DELETEDescription:Deletes a blog post by its ID.
DELETE `http://127.0.0.1:8000/api/blogposts/3/`
You can test the API using tools like Postman or curl.
Example curl command for fetching blog posts:
curl -X GET http://127.0.0.1:8000/api/blogposts/Whenever changes are made to your models, you need to apply migrations to update the database schema.
Create migrations:
python manage.py makemigrationsApply migrations:
python manage.py migrateTo start the development server, run the following command:
python manage.py runserverThe API will be accessible at:
http://127.0.0.1:8000/
Django:For creating the project and managing the application.Django REST Framework (DRF):To create the RESTful API.SQLite (default):For database management (it can change to PostgreSQL/MySQL as needed).
I welcome contributions to improve this project! If you want to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/feature-name). - Commit your changes (
git commit -m "Add new feature"). - Push the branch (
git push origin feature/feature-name). - Open a pull request.