This is a powerful and secure starter project for building RESTful APIs with Django. It comes pre-configured with a modern technology stack and essential features to get you up and running quickly.
- Modern Authentication: Uses JSON Web Tokens (JWT) for secure, stateless authentication (
djangorestframework-simplejwt). - Custom User Model: A flexible and extensible custom user model is ready from the start, using email as the primary identifier.
- API Documentation: Automatic, interactive API documentation powered by Swagger (drf-yasg).
- Environment-based Configuration: All sensitive keys and settings are loaded from a
.envfile for better security. - Custom Management Commands: Includes a command to create a superuser from environment variables, perfect for deployment.
- Social Authentication Hooks: Includes skeletons for Google and Facebook social authentication.
- CORS Ready: Pre-configured with
django-cors-headersto allow frontend integrations.
Follow these instructions to get the project set up and running on your local machine.
- Python 3.8+
virtualenv(or another virtual environment tool)
-
Clone the Project
git clone [https://github.com/morshedmasud/django-rest-framework-mysql-boilerplate.git](https://github.com/morshedmasud/django-rest-framework-mysql-boilerplate.git) cd django-rest-framework-mysql-boilerplate -
Create and Activate a Virtual Environment
python3 -m venv venv source venv/bin/activateOn Windows, use
venv\Scripts\activate -
Install Dependencies
pip install -r requirements.txt
-
Set Up Environment Variables
Create a
.envfile in the project root by copying the example file:cp .env.example .env
Now, open the
.envfile and fill in the required values. At a minimum, you need to generate aSECRET_KEY.Generate a Secret Key: You can generate a new secret key using the built-in Django utility:
python generate_key.py
Copy the output and paste it as the value for
SECRET_KEYin your.envfile.Your
.envfile should look like this:# SECURITY SECRET_KEY=your_newly_generated_secret_key_here DEBUG=1 ALLOWED_HOSTS=127.0.0.1,localhost # ADMIN USER CREDENTIALS ADMIN_EMAIL=[email protected] ADMIN_PASSWORD=YourSecurePassword123 # DATABASE (Defaults to SQLite) # DB_NAME=your_db # DB_USER=your_user # ...
-
Run Database Migrations This will create the necessary database tables, including those for the custom user model.
python manage.py migrate
-
Create a Superuser This command will create an admin account using the credentials you set in your
.envfile.python manage.py create_super_user
-
Run the Development Server
python manage.py runserver
The API will now be running at
http://127.0.0.1:8000/.
Once the server is running, you can interact with the following key endpoints:
- Admin Panel:
http://127.0.0.1:8000/admin/ - API Documentation (Swagger):
http://127.0.0.1:8000/swagger/
- Register a new user:
POST /api/register/- Body:
{ "full_name": "John Doe", "email": "[email protected]", "password": "yourpassword" }
- Obtain JWT Tokens:
POST /api/login/- Body:
{ "email": "your_email", "password": "your_password" } - Returns: An
accessandrefreshtoken.
- Refresh Access Token:
POST /api/login/refresh/- Body:
{ "refresh": "your_refresh_token" }
To access protected endpoints, include the access token in the request header:
Authorization: Bearer <your_access_token>