Building a CRUD application using Python and Django

Building a CRUD application using Python and Django

Building a CRUD application using Python and Django - Django is a web application framework written in Python. If you want to create the CRUD application using Python and Django, it is enough to ...

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:

sudo apt-get install python-pip
sudo pip install virtualenv

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.

virtualenv venv

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

source venv/bin/activate
cd venv

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

pip install django==1.11.8
mkdir app && cd app
django-admin startproject crudapp

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

app
└── crudapp
    ├── crudapp
    │   ├── __init__.py
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    └── manage.py

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.

python manage.py runserver

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.

python manage.py startapp blog_posts

This will create the necessary files that we require.

First and foremost create the Model of our application.

# -*- coding: utf-8 -*-
    from __future__ import unicode_literals


from django.db import models


# Create your models here.
class blog_posts(models.Model):
    title = models.CharField(max_length=400)
    tag = models.CharField(max_length=50)
    author = models.CharField(max_length=120)


    def __unicode__(self):
        return self.title


    def get_post_url(self):
        return reverse('post_edit', kwargs={'pk': self.pk})

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

python manage.py makemigrations
python manage.py migrate

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

# -- coding: utf-8 --
    from future import unicode_literals

from django.shortcuts import render, redirect, get_object_or_404
from django.forms import ModelForm


from blog_posts.models import blog_posts
# Create your views here.


class PostsForm(ModelForm):
    class Meta:
        model = blog_posts
        fields = ['id', 'title', 'author']


def post_list(request, template_name='blog_posts/post_list.html'):
    posts = blog_posts.objects.all()
    data = {}
    data['object_list'] = posts
    return render(request, template_name, data)


def post_create(request, template_name='blog_posts/post_form.html'):
    form = PostsForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('blog_posts:post_list')
    return render(request, template_name, {'form': form})


def post_update(request, pk, template_name='blog_posts/post_form.html'):
    post = get_object_or_404(blog_posts, pk=pk)
    form = PostsForm(request.POST or None, instance=post)
    if form.is_valid():
        form.save()
        return redirect('blog_posts:post_list')
    return render(request, template_name, {'form': form})


def post_delete(request, pk, template_name='blog_posts/post_delete.html'):
    post = get_object_or_404(blog_posts, pk=pk)
    if request.method=='POST':
        post.delete()
        return redirect('blog_posts:post_list')
    return render(request, template_name, {'object': post})

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.

"""
    crudapp URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
"""
from django.conf.urls import url
from django.contrib import admin
from blog_posts import views


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.post_list, name='post_list'),
    url(r'^new$', views.post_create, name='post_new'),
    url(r'^edit/(?P<pk>\d+)$', views.post_update, name='post_edit'),
    url(r'^delete/(?P<pk>\d+)$', views.post_delete, name='post_delete'),
]

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

"""
    Crudapp URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
     https://docs.djangoproject.com/en/1.11/topics/http/urls/
 """
from django.conf.urls import url, include
from django.contrib import admin
from crudapp.views import home


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog_posts/', include('blog_posts.urls', namespace='blog_posts')),
    url(r'^$', home, name='home' ),
]

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:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    <title>Django CRUD application!</title>
  </head>
  <body>
    <div class="container">
        <h1>Blog Post List!</h1>
        <ul>
          {% for post in object_list %}
          <li><p>Post ID: <a href="{% url "blog_posts:post_edit" post.id %}">{{ post.id }}</a></p>
          <p>Title: {{ post.title }}</p>
            <a href="{% url "blog_posts:post_delete" post.id %}">Delete</a>
          </li>
          {% endfor %}
        </ul>

    &lt;a href="{% url "blog_posts:post_new" %}"&gt;New Blog post entry&lt;/a&gt;
&lt;/div&gt;

</body> </html>

templates/blogposts/postform.html

<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
        <title>Django CRUD application!</title>
      </head>
      <body>
        <div class="container">
          <form  method="post">{% csrf_token %}
            {{ form.as_p }}
            <input class="btn btn-primary" type="submit" value="Submit" />
          </form>
        </div>
      </body>
    </html>

templates/blogposts/postdelete.html

<!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
        <title>Django CRUD application!</title>
      </head>
      <body>
        <div class="container">
          <form method="post">{% csrf_token %}
            Are you sure you want to delete "{{ object }}" ?
            <input class="btn btn-primary" type="submit" value="Submit" />
          </form>
        </div>
      </body>
    </html>

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

The final project tree should look like following:

crudapp
├── blog_posts
│   ├── admin.py
│   ├── apps.py
│   ├── init.py
│   ├── migrations
│   ├── models.py
│   ├── templates
│   │   └── blog_posts
│   │       ├── post_delete.html
│   │       ├── post_form.html
│   │       └── post_list.html
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
├── crudapp
│   ├── init.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   ├── wsgi.py
├── db.sqlite3
├── manage.py
└── requirements.txt

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

Originally published by  Nitin Prakash at  zeolearn.com

===================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

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

☞ Python for Time Series Data Analysis

☞ Python Programming For Beginners From Scratch

☞ Python Network Programming | Network Apps & Hacking Tools

☞ Intro To SQLite Databases for Python Programming

☞ Ethical Hacking With Python, JavaScript and Kali Linux

☞ Beginner’s guide on Python: Learn python from scratch! (New)

☞ Python for Beginners: Complete Python Programming



python django

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

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

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

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

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Developing Restful APIs with Python, Django and Django Rest Framework

This article is a definitive guide for starters who want to develop projects with RESTful APIs using Python, Django and Django Rest Framework.

How to run a python script in django propject which has multiple django models in it

I have a script that I want to run from terminal inside a django project and that script is a python script and it has multiple django models in it .So how can I run that script since I am not able to run it directly.Also everything resides inside docker container.