Django CRUD Web Application
In this tutorial, we are going to build a simple but beautiful CRUD application using the Python Web Development Framework called Django.
To follow along with this tutorial, clone the source code from this Github repository to your local machine. If this is your first time using Django, you are covered.
Installations and Setting Up Django
If you would like to do this project in a virtual environment, I’m sure there are many tutorials to help you create one. After you have created a virtual environment, proceed with this tutorial. If you do not care about virtual environment stuffs, also proceed.
- Run the command below to install Django (consider running sudo if you are not doing this in a virtual environment)
$ pip install Django==2.0.5
2. Verify the Django version installed by running the command below:
$ django-admin --version
Creating the Project
- Let us now create a directory for our Django projects with
$ mkdir django_projects
2. Move into this directory with
3. Create the project with
$ django-admin startproject CRUD
$ cd CRUD
4. Run the development server
$ python manage.py runserver
You should see something like this
5. Navigate to http://localhost:8000/ on your browser and you should see something like this:
Congratulations!!!! You are now ready to start building the CRUD app
Building the APP
Let us create an app called Crud in the CRUD project. Close the development server with Ctrl + C
Create the app with
$ python manage.py startapp Crud
So far, we have a directory structure like this
Open the settings.py file to specify the allowed hosts as well as add the Crud app to installed apps. You should have something like the below:
The Post Model
For our Crud app, we would like to be able to Create, Modify, View and Delete a post. Let us now define a post model in our models.py file. Open the models.py file in your favorite text editor and paste the below codes:
from django.db import models
title = models.CharField(max_length=120, help_text='title of message.')
message = models.TextField(help_text="what's on your mind ...")
Our post has a title and a message as fields.
Run the commands below to make migrations:
$ python manage.py makemigrations
$ python manage.py migrate
Let us now create a superuser to manage our application
Run the following in the terminal:
$ python manage.py createsuperuser
You will be prompted to enter a username, email address, password and password confirmation. If the superuser is successfully created, we can now run the development server and log on to the Django admin page. Run the development server with
$ python manage.py runserver
Navigate to http://localhost:8000/admin on your browser and you should see something similar as below:
Enter the details to log in.
You should see a page similar to that below:
Our post is not displayed here since we have not registered it in the admin.
Open the admin.py file in a text editor and add the following codes:
from django.contrib import admin
from .models import Post #add this to import the Post model
admin.site.register(Post) #add this to register the Post model
Refresh the page again and this time you should see the post appear as below
You can click on Add in front of Posts to add some posts.
On my next post, I will continue with the Views, URLS, Templates, and adding some finishing touches to our app. Don’t hesitate to put your comments below, if you have anything I should know, and I will be grateful.