-
Clone the repository:
git clone https://github.com/your-username/django-boilerplate.git
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
.\venv\Scripts\activate
- On macOS and Linux:
source venv/bin/activate
- On Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Create a postgres database for the project and add the credentials to the
.env
file.
createdb -U postgres database_name
Make sure you have PostgresSQL installed on your system for this to work.
-
Create
.env
file and add copy the content from.env.example
. Replace the database credentials with the ones you created in the previous step. -
Create migrations and migrate:
python manage.py makemigrations
python manage.py migrate
- Create a superuser:
python manage.py createsuperuser
- Run the development server:
python manage.py runserver
- You can now access the admin panel at
http://127.0.0.1:8000/admin/
and the API athttp://127.0.0.1:8000/api/
.
To create a new app, run the following command:
python manage.py startapp app_name
I like to organize my apps in the following structure:
django_backend/
apps/
app_name/
__init__.py
models/
__init__.py
models_1.py
models_2.py
...
api/
__init__.py
routes_1.py
routes_2.py
...
serializers/
__init__.py
serializers_1.py
serializers_2.py
...
services/
__init__.py
services_1.py
services_2.py
...
admin.py
apps.py
urls.py
... other files you may need
To quickly create the structure above, copy paste this into your terminal (replace app_name
with your app name):
mkdir app_name/{models,serializers,api,services}
rm -rf app_name/{models.py,views.py}
touch app_name/urls.py app_name/{models,serializers,api,services}/__init__.py
Don't forget to add your app to the INSTALLED_APPS
in django_backend/settings.py
.
INSTALLED_APPS = [
...
'app_name',
...
]