Django CRUD with Forms and Bootstrap Template

Adi Ramadhan
3 min readJul 21, 2020

--

Simple function based view Django CRUD with Django Forms and Bootstrap template.

Django Forms Official docs here.

Step 1: Preparation, Create Django Project, Initial Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.0
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create Django Apps
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

Step 3: Set up Templates Folder
Create folder templates in myapp

Add template directory in TEMPLATES parameter in myproject/settings.py

os.path.join(BASE_DIR, 'myapp', 'templates')

Step 4: Create Model in myapp, Makemigrations and Migrate
Add Book Model in myapp/models.py

myapp/models.py

Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate

Step 5: Create HTML Files in myapp/templates Folder
Create bootstrap base, create myapp/templates/base.html CDN Bootstrap.

myapp/templates/base.html

Create book-list template, myapp/templates/book-list.html

myapp/templates/book-list.html

Create book-create template, myapp/templates/book-create.html

myapp/templates/book-create.html

Create book-update template, myapp/templates/book-update.html

myapp/templates/book-update.html

Step 6: Create Forms.py in myapp Folder
Create forms in myapp, myapp/forms.py

myapp/forms.py

Step 7: Create Function Based View in myapp/views.py
Update myapp/views.py. Add Function Based View method that render html file and using BookForm.

myapp/views.py

Step 8: Setup URLs in myapp and myproject Folder
Create myapp/urls.py

myapp/urls.py

Update myproject/urls.py

myproject/urls.py

Step 9: Run Server and Testing
Run Server: python manage.py runserver
Testing: http://127.0.0.1:8000/book-list
Try to Create, Update, and Delete

Summarized Section

Step 1: Preparation, Create Django Project, Initial Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.0
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate

Step 2: Create Django Apps
Create apps: python manage.py startapp myapp
Add myapp to INSTALLED_APPS in myproject/settings.py

Step 3: Set up Templates Folder
Create folder templates in myapp
Add template directory in TEMPLATES parameter in myproject/settings.py

os.path.join(BASE_DIR, 'myapp', 'templates')

Step 4: Create Model in myapp, Makemigrations and Migrate
Add Book Model in myapp/models.py
Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate

Step 5: Create HTML Files in myapp/templates Folder
Create bootstrap base, create myapp/templates/base.html CDN Bootstrap.
Create book-list.html, book-create.html, book-update.html

Step 6: Create Forms.py in myapp Folder
Create forms in myapp, myapp/forms.py

Step 7: Create Function Based View in myapp/views.py
Update myapp/views.py. Add Function Based View method that render html file and using BookForm.

Step 8: Setup URLs in myapp and myproject Folder
Create myapp/urls.py
Update myproject/urls.py

Step 9: Run Server and Testing
Run Server: python manage.py runserver
Testing: http://127.0.0.1:8000/book-list
Try to Create, Update, and Delete

--

--