Lost your password?
Don't have an account? Sign Up

The Fun part — Understanding the Framework

Eniola Medunoye

Django, is a free and open source server-side python web development framework that makes building web applications faster and easy. With Django, you can build and maintain high quality and scalable web applications with minimal stress.

Django includes support for common Web development tasks to make developers focus on the fun side of building web applications rather than spending so much time reinventing the wheel. Those tasks include:

  • User Authentication
  • Content Administration
  • Routing, templates and Views
  • site maps
  • RSS feeds
  • And so on.

Understanding Django’s Basic Architecture

Django follows closely the MVC architectural pattern that separates an application into three main logical components — Model, View and Controller.

Want to read this story later? Save it in Journal.

  • Model : This contains all data related logic in an application. It could be data the user works with or data sensitive to the application itself
  • View : This represents how data is presented to the user. It contains all User Interface logic.
  • Controller : Controllers act as an interface between Model and View components. We can say controller is the way in which our Model and View interacts with each other.

Understanding Django’s architectural pattern makes it easy to understand the implementation of the code. Django’s architecture consists of three major parts:

  • The first part is a set of tools that make working with data and databases much easier
  • The second part is a plain-text templating system that can be used by non-programmers; and
  • The third part is a framework that handles communication between the user and the database and automates many of the stressful parts of managing a complex website.

It can be said that Django is an MVC framework — after all it follows same principle. While this is not entirely wrong, it isn’t quite right either. A common way of explaining Django’s architecture in terms of MVC is to describe it as a Model-Template-View (MTV) or Model-View-Template (MVT). For a further abstraction of the MVT pattern, they are discussed below.

Django’s MVT pattern as illustrated in djangobook.com

Model : A definitive source of information about a data. It contains the essential fields and behaviors of the data being stored. This is basically the same as the model in the MVC architecture.

View : Django View focuses not on how the data looks, but which data is presented to a user. As opposed to the View layer in the MVC architectural pattern, the Django view contains arbitrary logic necessary to return a web response — which could be contents of a web page or a 404 error. This layer also handles interactions between the template and the model.

Template : You probably can now guess what this does, the template layer deals with how data is being presented to the user. This layer basically controls what should be displayed and how it should be displayed to the user.

The view leads people to categorize Django as an MVC framework. The view in Django is most often described as being equivalent to the controller in MVC, but it’s not — it’s still the view.

A better image on how the view interacts with the Model and Template

Where does the “controller” fit in, then? In Django’s case, it’s probably the framework itself : the machinery that sends a request to the appropriate view, according to the Django URL configuration.

Django Folder Structure and Functions

To begin this Section, we first need to create a simple Django web application. To do so, we need to have the following installed on our respective machines — Python, PIP and Django.
Note : This tutorial is not a “Getting started with Django Tutorial”. So we are skipping the installation parts and assuming you have them all installed already.

We create the project with the “django-admin startproject mydjangoproject” command.

django-admin is Django’s command-line utility for administrative tasks.

After the project is created. navigate into the project’s directory

The project created should have a folder structure looking like

Here, we can see a folder with the same name as our root folder. This is a folder Django creates for you when you run the command to create a project. Inside contains a couple of files that are quite useful.

  • The __init.py__ file tells Python that this folder (your Django app) is a Python package.
  • asgi.py enables ASGI compatible web servers to serve your project (Django 3 only).
  • The settings.py contains the settings for our Django project. Every Django project must have a settings file.
  • urls.py contains project-level URL configurations.
  • wsgi.py enables WSGI compatible web servers to serve your project

manage.py is a thin wrapper that helps put our project’s package on sys.path and also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to our project’s settings.py file.

For a list of all django-admin and manage.py functionalities, click here.

Let us proceed to creating an app. Run the following command “python manage.py startapp myapp”

Hoping that ran successfully, we can now take a look at our app’s structure. Navigate into the app’s directory. The whole project structure should be looking like

  • The Migrations folder is where Django stores migrations, or changes to your database.
  • Init.py : This is what notifies python that our application is a django application
  • admins.py : This is where registration of our apps models with the Django administration occurs
  • models.py : Here, where we define and include database schemas and data related logic
  • tests.py : Contains test procedures that runs when testing our application.
  • views.py : Is the module containing the views for your app.

Now, we have a clear picture of how a django project structure looks but we must note, our application is not limited to these files only. As we build larger programs, we would feel the need to include more files that django do support. These files include urls.py, forms.py, serializers.py and so on(check the official django documentation to see all of these files and their functionalities).

Hopefully this tutorial has been able to shed more light into the beautiful world of Django. Never forget, Django is just a tool, and how we use it is all that matters .😊😊

To read more on the topic, you can check out this link

Source link


Leave a Comment

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