Building a CRUD Application using Django

Building a CRUD Application using Django

In this tutorial, we are going to build a simple but beautiful CRUD application using the Python Web Development Framework called Django.

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.

  1. 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

  1. Let us now create a directory for our Django projects with
$ mkdir django_projects

2. Move into this directory with

cd django_projects

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 CRUD 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
class Post(models.Model):
 title = models.CharField(max_length=120, help_text='title of message.')
 message = models.TextField(help_text="what's on your mind ...")

def str(self): return self.title

Our post has a title and a message as fields.

Make Migrations

Run the commands below to make migrations:

$ python manage.py makemigrations
$ python manage.py migrate

Superuser

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

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Learn More

Complete Python Bootcamp: Go from zero to hero in Python 3

Machine Learning A-Z™: Hands-On Python & R In Data Science

Python and Django Full Stack Web Developer Bootcamp

Complete Python Masterclass

The Python Bible™ | Everything You Need to Program in Python

Python and Django Full Stack Web Developer Bootcamp

Build a Backend REST API with Python & Django - Advanced

Python Django Dev To Deployment

Build Your First Python and Django Application

How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 16.04

Building a Universal Application with Nuxt.js and Django

Django Tutorial: Building and Securing Web Applications

Node.js vs. Django

Building a Weather App in Django

Originally published on https://medium.com

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.