Table of contents
1.
Introduction
2.
Creating an App in Django
3.
Setup
3.1.
Modules Required
3.2.
Basic Setup
4.
Creating the app
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Build Web App using Django

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Django is a free and open-source web framework based on Python that follows the model–template–views architecture and allows for the quick creation of safe and maintainable websites. Furthermore, Django is adaptable and can be used to build any website, such as a content management system, while also preventing numerous security blunders by offering vulnerability prevention. Additionally, it is portable and does not bind developers to a certain server platform.

In this tutorial, we'll look at how to make a Django project. So, let's begin creating our first Django project.

Creating an App in Django

Today, we'll make a todo app to learn the fundamentals of Django. This web app allows users to write notes similar to Google Keep or Evernote. 

Setup

Make sure you have Django installed and your basic setup is complete before proceeding with the next instructions. If you haven't installed Django, then read the following blog about Django Basics for building a project in Django.

Modules Required

django : django install

crispy_forms :

pip install --upgrade django-crispy-forms 

Basic Setup

Start a project by using the command :

django-admin startproject todo_web

Change directory to todo_web :

cd todo_web

Start the server by entering the following command in the terminal :

python manage.py runserver

To see if the server is up and running, open a web browser and type http://127.0.0.1:8000/ into the address bar.

Now press ctrl+c to bring the server to a halt.

Now, since we have the basic setup ready, let’s go and create the application.

Creating the app

Give command

python manage.py startapp todo

Go to todo folder using

cd todo

Using a text editor, open the project folder. The directory structure should look :
 

In settings.py, add “todo” app and “crispy_form” to your todo_web :
 

Edit urls.py file in todo_web :

from django.contrib import admin
from django.urls import path
from todo import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('del/<str:item_id>', views.remove, name='del'),
    path('', views.index, name="todo"),
]
You can also try this code with Online Python Compiler
Run Code

Edit models.py in todo : 

from django.db import models
from django.utils import timezone

class Todo(models.Model):
    title = models.CharField(max_length=100)
    details = models.TextField()
    date = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return self.title
You can also try this code with Online Python Compiler
Run Code

Edit views.py in todo :

from django.shortcuts import render, redirect
from django.contrib import messages 
from .forms import TodoForm
from .models import Todo
def index(request):
    item_list = Todo.objects.order_by("-date")
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('todo')
    form = TodoForm()
    page = {
             "forms" : form,
             "list" : item_list,
             "title" : "TODO LIST",
           }
    return render(request, 'todo/index.html', page) 
def remove(request, item_id):
    item = Todo.objects.get(id=item_id)
    item.delete()
    messages.info(request, "item removed !!!")
    return redirect('todo')
You can also try this code with Online Python Compiler
Run Code

Create a forms.py in todo :

from django import forms
from .models import Todo
 
class TodoForm(forms.ModelForm):
    class Meta:
        model = Todo
        fields="__all__"
You can also try this code with Online Python Compiler
Run Code

Register models to admin.py

from django.contrib import admin
from .models import Todo
# Register your models here.
admin.site.register(Todo)
You can also try this code with Online Python Compiler
Run Code

Go to templates/todo/index.html and make changes as you wish, you may refer to the file: index.html

Make migrations and migrate it using the below commands :

python manage.py makemigrations
python manage.py migrate

You may now start the server and see your todo app.

python manage.py runserver

 

Practice this code with the help of Online Python Compiler

FAQs

  1. What are the advantages of Django?
    Implemented in Python, Better CDN connectivity and Content Management, Batteries Included Framework, Fast Processing.,Offers Rapid-development, Scalable, Security.
     
  2. What are the disadvantages of Django?
    No conventions, Most programmers dislike Django web development because of the lack of conventions, Not for smaller projects, Monolithic framework, Steep learning curve, Multiple request issues.

Key Takeaways

  • Cheers if you reached here! In this blog, we learned to develop a simple todo app using Django. 
  • This app helped us learn several Django fundamentals and gave a basic idea of this python framework.

This is not it to Django instead it's a mere beginning to the framework.
The Django framework is an excellent location for novice Python web developers to start because the official documentation and tutorials are among the finest in the field.

Keep learning Ninjas!

Live masterclass