Building Web Applications in Django from Scratch : Part 1

Devang Mehta
4 min readFeb 17, 2023

--

understanding Django basics and creating a project in Django

Django is a free and open source high level python framework that helps in creating complex data driven applications. It is immensly popular in the world of software development and provides high level of flexibility and scalability. The built-in features and tools that have been incorporated in django makes it easy to use and deployment ready. What I mean by this is that with django we can build applications very quickly and make them production ready.

Some of the most famous features of django include ORM, middlewares, admin interface, session handling, etc which we shall learn in this series of blogs. Django also offers high security and SEO optimization apart from the features mentioned earlier.

With this background let’s start diving deep into the Django universe, starting with understanding the very basics of it.

Architechture of Django

The Django web framework follows the MVT(Model, View, Template) architechture which helps in packaging all the components of a traditional web application into a single framework. It contains the capability to handle database operations, logic processing and rendering the user interface.

  • Model: Model defines the structure and behaviour of the database. Each app inside the django project contains a models.py file where all teh database tables and logics are written. Each model in the models.py file is a table in the database and each attribute of the model is a field associated with the table.
  • View: The view is responsible for handling all the logic processing. It serves the incoming requests with apt responses after completing all the necessary steps required to generate the response. If there is a need to interact with the database, the view is responsible for satisfying those needs.
  • Template: The template can be defined as the component with which the user interacts. It is the user interface using which the user sends requests to corresponding views and shows the responses accordingly.

Requirements

  • Python
  • Django

Creating a Project

Django provides an easy setup for building projects from scratch. It provides pre-defined settings, libraries and a project structure that helps in organizing and working with the project. The django-admin command that comes with Django allows developers to perform various admin operations and we will be creating our project with the same.

To create a new project, type the below command in the terminal of your choice:

django-admin startproject myproject

This will create myproject directory in your current directory. There are various files that are automatically created within the myproject directory and we shall first look at their use.

myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
  • The outer myproject/ root directory is a container for your project. It will contains all the apps and settings that you will be creating in this project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: This is a command-line utility that lets you interact witht he Django project.
  • The inner myproject/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it.
  • myproject/__init__.py: An empty file that tells Python that this directory should be considered a Python package
  • myproject/settings.py: This file contains all the configurations related to the Django project
  • myproject/urls.py: This file contains the url declarations for the Django project. It helps in mapping the incoming requests to correct apps and their views.
  • myproject/asgi.py: An entry-point for ASGI-compatible web servers to serve the Django project
  • myproject/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project

Creating an App

Each app that we create in Django has a specific convention and directory structure. Django automatically generates the basic structure of the app which contains all the files required to work with the app.

To create a new app we use the manage.py command line utility.

python manage.py startapp myapp

The above command will create an app called myapp and set up its directory structure as below

myapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

Now that we have setup everything that we need to start working on our new Django project, in the next part of this article, we shall discuss how to prepare our app with all the logical code and database operations.

If you liked this article, do follow me for more contents like this. I have another blog at https://devangmehta1997.wordpress.com/ which you can follow for similar content.

Thank you 🙏

--

--

Devang Mehta

Engineering Lead | Interested in tech, history, philosophy and psychology