This is a simple template for creating RESTful APIs with Django and Django REST Framework.
- Simple structure for organizing Django apps and REST API
- Basic setup with model, serializer, view, and routing
- Support for environment variable configuration
django-api-template/
├── django_api_template/ # Main Django project directory
│ ├── __init__.py
│ ├── settings.py # Configuration for your Django project
│ ├── urls.py # URL routing
│ ├── wsgi.py # WSGI application for deployment
├── api/ # Django app containing the API logic
│ ├── migrations/ # Database migrations
│ ├── __init__.py
│ ├── admin.py # Admin panel configuration
│ ├── apps.py # App configuration
│ ├── models.py # Database models
│ ├── serializers.py # Serializers for converting data
│ ├── tests.py # Unit tests for the app
│ └── views.py # API views
├── manage.py # Command-line utility for Django
├── .env # Environment variables
├── requirements.txt # Python package dependencies
├── .gitignore # Files to be ignored by Git
└── README.md # Project documentation
- Python 3.x: Download from python.org.
- pip: Comes with Python; ensure it’s updated.
-
Clone the repository:
git clone https://github.com/yourusername/django-api-template.git cd django-api-template
-
Create and activate a virtual environment:
python -m venv venv # Create virtual environment # Activate the virtual environment # Windows venv\Scripts\activate # macOS/Linux source venv/bin/activate
-
Install requirements:
pip install -r requirements.txt
-
Set up environment variables:
Create a
.env
file in the root directory (or modify the existing one) to configure any necessary environment variables:SECRET_KEY=your-secret-key DEBUG=True
-
To run the Django development server, use:
python manage.py runserver
The application will be accessible at
http://127.0.0.1:8000/
by default.
- To add new models and APIs, create them in the
api/models.py
,api/serializers.py
, andapi/views.py
files. Be sure to also update the routes inurls.py
.
-
After creating or modifying models, run the following commands to create and apply database migrations:
python manage.py makemigrations python manage.py migrate
-
You can create tests for your application in the
api/tests.py
. Run your tests with:python manage.py test
Modify and expand this template for your project needs. Ensure to update the CORS settings and API routes as per your application's requirements.
Feel free to fork this repository, make enhancements, and submit pull requests. Contributions are welcome to improve features and add new ones.
This project is licensed under the MIT License - see the LICENSE file for details.