Tutorial for building create, retrieve, update and delete website application with Django and SQLite (default django database)
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Make sure you have installed Python 3 and virtual environment on your device
File structure in django by default has a structure like below
* django-crud-sqlite/
|--- rattlesnake/
| |--- app/
| | |--- migrations/
| | |--- templates/
| | |--- __init__.py
| | |--- admin.py
| | |--- apps.py
| | |--- models.py
| | |--- tests.py
| | |--- views.py
| |--- rattlesnake/
| | |--- __init__.py
| | |--- settings.py
| | |--- urls.py
| | |--- wsgi.py
| |--- manage.py
|--- venv/
A step by step series of examples that tell you how to get a development env running
- Create virtual environment and activate inside your
django-crud-sqlite/
directory according the above structure
virtualenv venv
> On windows -> venv\Scripts\activate
> On linux -> . env/bin/activate
- Install django and start new project inside your
django-crud-sqlite/
directory according the above structure
pip install django
django-admin startproject rattlesnake
cd rattlesnake
- Create new app, from
rattlesnake/
directory will create create newapp/
to store the collection
> On Windows -> manage.py startapp app
> On Linux, etc -> ./manage.py startapp app
- Register your app into
rattlesnake
project, theapp
toINSTALLED_APP
inrattlesnake/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
:
'app',
:
]
- Create the model to define the table structure of database and save the collection into database
app/models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=200, null=False)
identityNumber = models.CharField(max_length=200, null=False)
address = models.CharField(max_length=200, null=True)
department = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
# The absolute path to get the url then reverse into 'student_edit' with keyword arguments (kwargs) primary key
def get_absolute_url(self):
return reverse('student_edit', kwargs={'pk': self.pk})
- Every after change
models.py
you need to make migrations intodb.sqlite3
(database) to create the table for the new model
manage.py makemigrations
manage.py migrate
- Create the views to create app pages on browser, the file is
app/views.py
according the above structure
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from .models import Student
# Create your views here.
class StudentList(ListView):
model = Student
class StudentDetail(DetailView):
model = Student
class StudentCreate(CreateView):
model = Student
# Field must be same as the model attribute
fields = ['name', 'identityNumber', 'address', 'department']
success_url = reverse_lazy('student_list')
class StudentUpdate(UpdateView):
model = Student
# Field must be same as the model attribute
fields = ['name', 'identityNumber', 'address', 'department']
success_url = reverse_lazy('student_list')
class StudentDelete(DeleteView):
model = Student
success_url = reverse_lazy('student_list')
- Then, create file
app/urls.py
to define app url path (in CI as same as route function)
from django.urls import path
from . import views
urlpatterns = [
path('', views.StudentList.as_view(), name='student_list'),
path('view/<int:pk>', views.StudentDetail.as_view(), name='student_detail'),
path('new', views.StudentCreate.as_view(), name='student_new'),
path('edit/<int:pk>', views.StudentUpdate.as_view(), name='student_edit'),
path('delete/<int:pk>', views.StudentDelete.as_view(), name='student_delete'),
]
- The
app/urls.py
would not work unless you include that into the main urlrattlesnake/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
:
path('student/', include('app.urls')),
:
]
- Create the html file to display user interface, you need create directory
app/templates/app/
like below
* django-crud-sqlite/
|--- rattlesnake/
| |--- app/
| | |--- migrations/
| | |--- templates/
| | | |--- app/
| | |--- __init__.py
| | |--- admin.py
| | |--- apps.py
| | |--- models.py
| | |--- tests.py
| | |--- views.py
| |--- rattlesnake/
| | |--- __init__.py
| | |--- settings.py
| | |--- urls.py
| | |--- wsgi.py
| |--- manage.py
|--- venv/
- Create file
app/templates/app/student_list.html
to display or parsing student list data withListView
library
<h1>Student List</h1>
<a href="{% url 'student_new' %}">Create New Student</a><br><br>
<table border="1">
<tr>
<th>Name</th>
<th>Identity Number</th>
<th>Action</th>
</tr>
{% for student in object_list %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.identityNumber }}</td>
<td>
<a href="{% url 'student_detail' student.id %}">Detail</a>
<a href="{% url 'student_edit' student.id %}">Edit</a>
<a href="{% url 'student_delete' student.id %}">Delete</a>
</td>
</tr>
{% empty %}
<tr><td colspan="3"><b>Data is empty! Please, add data first.</b></td></tr>
{% endfor %}
</table>
- Create file
app/templates/app/student_detail.html
to display or parsing data of each student and will used byDetailView
library
<h1>Student Detail</h1>
<h3>Name : {{ object.name }}</h3>
<h3>Identity Number : {{ object.identityNumber }}</h3>
<h3>Address : {{ object.address }}</h3>
<h3>Department : {{ object.department }}</h3>
- Create file
app/templates/app/student_form.html
to display form input and edit views
<h1>Student Form</h1>
<form method="POST">{% csrf_token %}
<table>
<tr>
<td>Name</td>
<td>:</td>
<td>{{ form.name }}</td>
</tr>
<tr>
<td>Identity Number</td>
<td>:</td>
<td>{{ form.identityNumber }}</td>
</tr>
<tr>
<td>Department</td>
<td>:</td>
<td>{{ form.department }}</td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td>{{ form.address }}</td>
</tr>
<tr>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
- Create file
app/templates/app/student_confirm_delete.html
to display promt or alert confirmation to delete the object view
<form method="post">{% csrf_token %}
Are you sure you want to delete "{{ object }}" ?
<input type="submit" value="Submit" />
</form>
- Test the project
manage.py runserver
* django-crud-sqlite/
|--- rattlesnake/
| |--- app/
| | |--- migrations/
| | |--- templates/
| | | |--- app/
| | | | |--- student_confirm_delete.html
| | | | |--- student_detail.html
| | | | |--- student_form.html
| | | | |--- student_list.html
| | |--- __init__.py
| | |--- admin.py
| | |--- apps.py
| | |--- models.py
| | |--- tests.py
| | |--- urls.py
| | |--- views.py
| |--- rattlesnake/
| | |--- __init__.py
| | |--- settings.py
| | |--- urls.py
| | |--- wsgi.py
| |--- db.sqlite3
| |--- manage.py
|--- venv/
- List student page but if list is empty will display
Data is empty! Please add data first.
- Form input student page, url path
student/new
- List student page if data inserted
- Student detail page, url path
student/view/<parameters:id>
- Form edit student page, url path `student/edit/parameter:id
- Confirmation page if data will remove from collection, url path
student/delete/<parameter:id>
- Python 3 - The language programming used
- Django 2 - The web framework used
- Virtualenv - The virtual environment used
- SQLite 3 - The database library
You can clone or download this project
> Clone : git clone https://github.com/piinalpin/django-crud-sqlite.git
- Alvinditya Saputra - Initial work - DSS Consulting - LinkedIn Instagram Twitter