How I Create Django Admin CRUD Apps
Spoiler: This is long read article, jump to Summarized Section in the end of article to read short version.
Step 1: Preparation:
I use python 3.8 in windows when writing this article. For installation instruction, you can read the official python installation document in this link.
check python version: python — version
Install python virtualenv: pip install virtualenv
create virtual environment: virtualenv env
activate virtual env: venv\Scripts\activate
Install Django in virtual environment: pip install django
Step 2: Create Django Project
create project: django-admin startproject myproject
This command will create a Django myproject folder with its generated folders and files.
Go to myproject folder: cd myproject
Start Django: python manage.py runserver
Try to access http://127.0.0.1:8000 in browser
Step 3: Initial Migration
Migrate is a step to generate database (by default django use sqlite database) and some tables. For initial, Django provides great initial tables for us. In previous step there is warning “You have 17 unapplied migration(s) … . Run ‘python manage.py runserver’ to apply them.” Let’s try to apply.
migrate: python manage.py migrate
We can restart apps and no more migration warning and django admin site is installed. Try to access admin site: http://127.0.0.1:8000/admin
Step 4: Superuser
Create superuser: python manage.py createsuperuser
Type username, email and password.
Try to login in Django admin with superuser
In this state we can explore django admin site with built-in menu (include authorization and authentication).
Step 5: Create Application
Create app: python manage.py startapp myapp
this command will generate myapp folder and some files.
Open myproject folder in text editor. I use VS Code. alternatives: Sublime, Atom, Notepad++.
Create new model in myapp/models.py
Register model in admin myapp/admin.py
Add myapp in INSTALLED_APPS in myproject/settings.py
Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate
Run server and try to access admin site. Books menu now can be accessed in admin site.
Open Books menu in MYAPP and do some cool operation like Add, Edit, Delete.
Summarized Section:
Step 1. Preparation
install virtualenv: pip install virtualenv
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django
Step 2. Create Django Project
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Run Django: python manage.py runserver
Access: http://127.0.0.1:8000
Step 3. Initial Migration
Migrate: python manage.py migrate
Step 4. Superuser
Create superuser: python manage.py createsuperuser
Admin access: http://127.0.0.1:8000/admin
Step 5. Create Application
Create app: python manage.py startapp myapp
Create model: add model in myapp/models.py
Create modeladmin: add modeladmin in myapp/admin.py
Register app: add myapp in INSTALLED_APP in myproject/settings.py
Make migrations: python manage.py makemigrations
Migrate: python manage.py migrate
Run server: python manage.py runserver