Skip to content

4urie/API-Programming-Exercise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Django REST API - Books Management

A simple Django REST API for managing books using Django REST Framework.

Features

  • CRUD operations for books
  • RESTful API endpoints
  • Django Admin interface
  • SQLite database (default)

Requirements

  • Python 3.8+
  • Django 5.2.6
  • Django REST Framework 3.16.1

Installation

1. Clone the repository

git clone https://github.com/4urie/API-Programming-Exercise
cd API

2. Create a virtual environment

# Windows
python -m venv myenv
myenv\Scripts\activate

# macOS/Linux
python3 -m venv myenv
source myenv/bin/activate

3. Install dependencies

pip install django==5.2.6
pip install djangorestframework==3.16.1

4. Navigate to the project directory

cd myproject

5. Apply database migrations

python manage.py makemigrations
python manage.py migrate

6. Create a superuser (optional)

python manage.py createsuperuser

7. Run the development server

python manage.py runserver

The API will be available at http://127.0.0.1:8000/

API Endpoints

Books API

  • GET /api/books/ - List all books
  • POST /api/books/ - Create a new book
  • GET /api/books/{id}/ - Retrieve a specific book
  • PUT /api/books/{id}/ - Update a specific book
  • PATCH /api/books/{id}/ - Partially update a specific book
  • DELETE /api/books/{id}/ - Delete a specific book

Book Model Fields

  • id - Auto-generated primary key
  • title - Book title (max 200 characters)
  • author - Book author (max 200 characters)
  • published_date - Publication date

Usage Examples

Create a new book

curl -X POST http://127.0.0.1:8000/api/books/ \
  -H "Content-Type: application/json" \
  -d '{
    "title": "The Great Gatsby",
    "author": "F. Scott Fitzgerald",
    "published_date": "1925-04-10"
  }'

Get all books

curl -X GET http://127.0.0.1:8000/api/books/

Get a specific book

curl -X GET http://127.0.0.1:8000/api/books/1/

Update a book

curl -X PUT http://127.0.0.1:8000/api/books/1/ \
  -H "Content-Type: application/json" \
  -d '{
    "title": "The Great Gatsby - Updated",
    "author": "F. Scott Fitzgerald",
    "published_date": "1925-04-10"
  }'

Delete a book

curl -X DELETE http://127.0.0.1:8000/api/books/1/

Admin Interface

Access the Django admin interface at http://127.0.0.1:8000/admin/ using the superuser credentials you created.

Project Structure

myproject/
├── api/
│   ├── migrations/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── myproject/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
└── README.md

Development

Running Tests

python manage.py test

Making Changes

  1. Make your changes to the code
  2. Create and apply migrations if you modify models:
    python manage.py makemigrations
    python manage.py migrate
  3. Test your changes
  4. Commit and push to GitHub

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

License

This project is open source and available under the MIT License.

Contact

About

Design your own API based on the video tutorials and links that were shared.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages