# Django Blog Application
## Introduction
This is a simple blog application built using Django and Django REST Framework. The application includes functionalities for creating, reading, updating, and deleting posts and comments. User authentication is handled using JSON Web Tokens (JWT).
## Features
- User registration and login
- Create, read, update, and delete posts
- Create and read comments for each post
- Like and unlike posts
- Token-based authentication
- Pagination for the list of posts
## Setup Instructions
### Prerequisites
- Python 3.x
- pip (Python package installer)
### Installation
2. Create a virtual environment:
```sh
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Apply migrations:
python manage.py makemigrations python manage.py migrate
-
Create a superuser:
python manage.py createsuperuser
-
Run the development server:
python manage.py runserver
- Endpoint:
/api/register/
- Method:
POST
- Payload:
{ "username": "newuser", "password": "newpassword", "email": "[email protected]", "first_name": "New", "last_name": "User" }
- Response:
{ "username": "newuser", "email": "[email protected]", "access": "your-access-token", "refresh": "your-refresh-token" }
- Endpoint:
/api/token/
- Method:
POST
- Payload:
{ "username": "newuser", "password": "newpassword" }
- Response:
{ "access": "your-access-token", "refresh": "your-refresh-token" }
- Endpoint:
/api/token/refresh/
- Method:
POST
- Payload:
{ "refresh": "your-refresh-token" }
- Response:
{ "access": "your-new-access-token" }
- Endpoint:
/api/posts/
- Method:
GET
- Response:
{ "count": 15, "next": "http://example.com/api/posts/?page=2", "previous": null, "results": [ { "id": 1, "title": "First Post", "content": "This is the content of the first post.", "author": "newuser", "published_date": "2023-07-21T12:34:56Z", "likes": [], "likes_count": 0 }, // ... 9 more posts ] }
- Endpoint:
/api/posts/
- Method:
POST
- Payload:
{ "title": "New Post", "content": "This is the content of the new post." }
- Response:
{ "id": 1, "title": "New Post", "content": "This is the content of the new post.", "author": "newuser", "published_date": "2023-07-21T12:34:56Z", "likes": [], "likes_count": 0 }
- Endpoint:
/api/posts/<int:pk>/
- Method:
GET
- Response:
{ "id": 1, "title": "First Post", "content": "This is the content of the first post.", "author": "newuser", "published_date": "2023-07-21T12:34:56Z", "likes": [], "likes_count": 0 }
- Endpoint:
/api/posts/<int:pk>/
- Method:
PUT
- Payload:
{ "title": "Updated Post", "content": "This is the updated content of the post." }
- Response:
{ "id": 1, "title": "Updated Post", "content": "This is the updated content of the post.", "author": "newuser", "published_date": "2023-07-21T12:34:56Z", "likes": [], "likes_count": 0 }
- Endpoint:
/api/posts/<int:pk>/
- Method:
DELETE
- Response:
204 No Content
- Endpoint:
/api/posts/<int:post_id>/comments/
- Method:
GET
To run the tests, execute the following command:
python manage.py test