top

Building a CRUD application using Python and Django

Introduction: I’ve been meaning to write a series on Django which is a web application framework written in Python. To follow this tutorial you don’t need to be a pro in python and have to know it inside-out. Just some basics will get you through it. Before we start writing applications, we must know a little about what is Django. Django is a web application framework that acts more of an MTV pattern instead of MVC. Think it this way: Model remains model View has been replaced by Templates Controller gets replaced by View A simple Hello-world application is just a few lines of code in Django! But moving from this simple thing a full fledged application can be a daunting task in itself. There are other concepts that can help proceed further such as ORM, migrations etc that help in building a bigger application. But for this tutorial we’ll be building a simple CRUD( Create, Retrieve, Update and Delete ) application. To get started with you need to have python and virtualenv installed on your machine. Python is already installed on the linux systems. But you'll need to install virtualenv. To install virtualenv follow the command: Application Structure Before we actually start writing code we need to get a hold of the application structure. We'll first execute several commands that are essential in django project development. After installing virtualenv, we need to set the environment up. We are setting a virtual environment of the name venv here. Now we need to activate it. Now that it has been activated. We need to start our project. Feed in the following command to start a project. The first line installs Django v1.11.8 and creates a directory named app in the parent directory. the third line starts a project named crudapp in the app directory. The directory tree should look like We'll see the meaning of each file and what it does one by one. But first, to test if you are going in the right directoion, run the following command. If you get an output like below then you're doing it right. Let's see what exactly the different files that we created mean. __init__.py : Acts as an entry point for your python project. settings.py : Describes the configuration of your Django installation and lets Django know which settings are available. urls.py : used to route and map URLs to their views. wsgi.py : contains the configuration for the Web Server Gateway Interface. The Web Server Gateway Interface (WSGI) is the Python platform standard for the deployment of web servers and applications. Writing the Application Now this is where we start coding our app. For this operation we'll consider blog post as our entity. We'll be applying CRUD operations to blog posts. The app in our project will be called blog_posts. This will create the necessary files that we require. First and foremost create the Model of our application. Now that we have our model ready, we'll need to migrate it to the database. Now we create our Views where we define each of our CRUD definition. Now that we have our Views, we create mappings to URL in our /crudapp/blog_posts/urls.py file. Make a note that the following is our app specific mappings. Now we create project specific mappings in /crudapp/crudapp/urls.py. Now almost everything is done and all we need to do is create our templates to test the operations. Go ahead and create a templates/blog_posts directory in crudapp/blog_posts/. templates/blog_posts/post_list.html: templates/blog_posts/post_form.html templates/blog_posts/post_delete.html Now we have all the necessary files and code that we require. The final project tree should look like following: Execute python manage.py runserver and voila!! You have your Django app ready.
Rated 4.0/5 based on 0 customer reviews
Normal Mode Dark Mode

Building a CRUD application using Python and Django

Nitin Prakash
Blog
06th Feb, 2018
Building a CRUD application using Python and Django

Introduction:

I’ve been meaning to write a series on Django which is a web application framework written in Python. To follow this tutorial you don’t need to be a pro in python and have to know it inside-out. Just some basics will get you through it.

Before we start writing applications, we must know a little about what is Django. Django is a web application framework that acts more of an MTV pattern instead of MVC. Think it this way:

  • Model remains model

  • View has been replaced by Templates

  • Controller gets replaced by View

A simple Hello-world application is just a few lines of code in Django! But moving from this simple thing a full fledged application can be a daunting task in itself. There are other concepts that can help proceed further such as ORM, migrations etc that help in building a bigger application. But for this tutorial we’ll be building a simple CRUD( Create, Retrieve, Update and Delete ) application.

To get started with you need to have python and virtualenv installed on your machine. Python is already installed on the linux systems. But you'll need to install virtualenv. To install virtualenv follow the command:

Application Structure

Before we actually start writing code we need to get a hold of the application structure. We'll first execute several commands that are essential in django project development.

After installing virtualenv, we need to set the environment up.

We are setting a virtual environment of the name venv here. Now we need to activate it.

Now that it has been activated. We need to start our project. Feed in the following command to start a project.

The first line installs Django v1.11.8 and creates a directory named app in the parent directory. the third line starts a project named crudapp in the app directory. The directory tree should look like

We'll see the meaning of each file and what it does one by one. But first, to test if you are going in the right directoion, run the following command.

If you get an output like below then you're doing it right.

Building a CRUD application using Python and Django

Let's see what exactly the different files that we created mean.

  • __init__.py : Acts as an entry point for your python project.

  • settings.py : Describes the configuration of your Django installation and lets Django know which settings are available.

  • urls.py : used to route and map URLs to their views.

  • wsgi.py : contains the configuration for the Web Server Gateway Interface. The Web Server Gateway Interface (WSGI) is the Python platform standard for the deployment of web servers and applications.

Writing the Application

Now this is where we start coding our app. For this operation we'll consider blog post as our entity. We'll be applying CRUD operations to blog posts.

The app in our project will be called blog_posts.

This will create the necessary files that we require.

First and foremost create the Model of our application.

Now that we have our model ready, we'll need to migrate it to the database.

Now we create our Views where we define each of our CRUD definition.

Now that we have our Views, we create mappings to URL in our /crudapp/blog_posts/urls.py file. Make a note that the following is our app specific mappings.

Now we create project specific mappings in /crudapp/crudapp/urls.py.

Now almost everything is done and all we need to do is create our templates to test the operations.

Go ahead and create a templates/blog_posts directory in crudapp/blog_posts/.

  • templates/blog_posts/post_list.html:

  • templates/blog_posts/post_form.html

  • templates/blog_posts/post_delete.html

Now we have all the necessary files and code that we require.

The final project tree should look like following:

Execute python manage.py runserver and voila!! You have your Django app ready.

Nitin

Nitin Prakash

Blog Author

I'm a software engineering student. I like to code in C++ and Python. I'm a freelancer and like to build web applications and softwares. Always looking forward to writing better codes.

Leave a Reply

Your email address will not be published. Required fields are marked *

Top comments

Shivangi

17 May 2018 at 11:24am
Thanks for sharing the descriptive information on Hadoop course. It’s really helpful to me since I'm taking Hadoop training. Keep doing the good work and if you are interested to know more on Hadoop, do check this Hadoop tutorial.https://www.youtube.com/watch?v=1jMR4cHBwZE

vijay

June, 24th at 12:05pm
i am not undewrstanding in crud concept sir.but i like coding is very much so i will learn lot so any other documentation u will help me sir.

SUBSCRIBE OUR BLOG

Follow Us On

Share on

other Blogs